Differences between Linux processes and jobs and related commands

Source: Internet
Author: User
Article Title: differences between Linux processes and jobs and related commands. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Everything running on a Linux system can be called a process. For example, a simple helloworld program, input the ls command on the terminal, and so on.

A normal running process is called a job. A job can start multiple processes. For example, the job ls-lrt | grep *. txt starts two processes.

Commands related to processes and jobs include kill, disown, wait, fg, bg, and jobs.

Fg, bg, and jobs only accept the job number as the parameter.

Kill, disown, and wait can accept the job number as the parameter and the acceptable process number as the parameter.

The jobs command displays the status of started jobs in the Current shell environment.

The kill command can be used to terminate a process.

[Alex @ cgdp alex] $ sleep 100 &

[1] 6273

[Alex @ cgdp alex] $ ps

PID TTY TIME CMD

6230 pts/0 00:00:00 bash

6273 pts/0 00:00:00 sleep (process to be deleted)

6274 pts/0 00:00:00 ps

[Alex @ cgdp alex] $ kill-9 6273

[Alex @ cgdp alex] $ ps

PID TTY TIME CMD

6230 pts/0 00:00:00 bash

6275 pts/0 00:00:00 ps

[1] + Killed sleep 100 (the process has been deleted)

The disown command can be used to delete a job.

[Alex @ cgdp alex] $ ls-l | sleep 200 &

[1] 6326

[Alex @ cgdp alex] $ jobs

[1] + Running ls -- color = tty-l | sleep 200 &

[Alex @ cgdp alex] $ disown % 1

[Alex @ cgdp alex] $ jobs

[Alex @ cgdp alex] $

The fg command puts the jobs running in the background to the foreground, and bg restarts a suspended job.

You can use CTRL + z to suspend the current process to the background, execute some other operations, and then use fg to re-return the suspended process to the foreground (also

Use bg to put the suspended process in the background) to continue running.

Run the wait command to stop the script until all the jobs running in the background are completed, or until the job with the specified job number or process number is finished.

Use the wait command to prevent the script from exiting before the background job is completed (this will generate an orphan process.

For example:

Write a simple script test

#! /Bin/sh

Ls-l &

Echo "done"

Run:

[Alex @ cgdp shell] $./test

Done

[Alex @ cgdp shell] $ total 4

-Rwxrwxr-x 1 alex 30 May 5 21:15 test

Use the wait command to override the Script test

#! /Bin/sh

Ls-l &

Wait

Echo "done"

Run:

[Alex @ cgdp shell] $./test

Total 4

-Rwxrwxr-x 1 alex 35 May 5 21:19 test

Done

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.