Job Management in Bash and bash job Management

Source: Internet
Author: User

Job Management in Bash and bash job Management

I was not prepared to write this blog, because Task Management (job Management) is very common, so I feel that there is no need to write such a thing. But if you think about it, record it. Maybe someone will use it.

I wonder if you have ever encountered such a situation. When you are eager to open VIM and write code to the right corner, the operating MM or product MM is panting: "hurry up and help me run the xx data for a PPT later ". However, you do not want to directly close the current VIM, and in some systems, you cannot open a new tty (such as setting the maxlogins parameter) to execute these tasks. What should you do? This actually involves job management.

So what is task management? As the name implies, it refers to adding, deleting, and processing multiple jobs in the same tty in Bash. Since every job in Bash is actually a subprocess of the current Bash, basically, job management refers to process management. Note that the above statement uses the word "the same tty". In this case, we cannot manage jobs in tty2 through tty1 job management. For example, on the tty3 terminal, place vim in the background using ctrl + z and pause it. In the current tty, run the jobs command to view the background task list, as shown in:


At this time, log on to the new tty and run the jobs command to check that no jobs are in the background:


Job Control

1. Run ctrl + z to throw the job to the background.

As we can see just now, during the vim editing process, you can press ctrl + z to pause the current VIM in the background, and the tty pause will output the following prompt:

[1]+  Stopped                 vim

[1] indicates the number of the job, which is used when the job is placed on the foreground or when the background process is killed.

Stopped indicates the status of the current process (in fact, jobs placed in the background using ctrl + z are all Stopped by default). vim refers to jobs placed in the background. Wait. What does the plus sign + mean? If we place two more jobs in the background, run the jobs command to view the jobs list in the current background. The output is as follows:


It is found that there are not only "+", but also. In fact, when you enter the fg command, the job marked as + will be taken to the foreground for execution by default, and the-number is the spare tire after the job is named +.

2. Run the command in the background &

In Bash, we often do some time-consuming work. For example, it may take more than 10 minutes to read the log script. Can't we keep staring at it all the time? In this case, it is best to add & to the end of the command and put it in the background for execution.

As a test, we enter the following content in test. php:

<?php$i = 0;while($i++ < 30){    echo $i;    sleep(1);}

Then, it is thrown into the background for execution:


We can see that the program output will still be output to the Bash standard output even if you press CTRL + C. This can be solved through redirection:

php test.php 2>&1 >>test.log &

Run the jobs command to view the status of jobs:


It is almost the same as the previous one, except that the Running state is thrown into the background this time. After the task is executed, the screen displays the following words:


Indicates that the current job has been completed.

3. Get the background work to the foreground to process fg (% jobnumber)

The previous section only describes how to place work in the background and how to view the background jobs status. Now that the data is running out, the Operation MM is gone. Continue to open the vim code in the background just now. This requires the fg command

Command Format: fg [% jobnumber]. The content in [] can be saved. By default, the job marked as + is retrieved. to retrieve the specified job, enter the job number:

fg %1

You can retrieve the job numbered 1.

4. Change the working status in the background to running bg (% jobnumber)

We know that jobs placed in the background through CTRL + Z are in the Stopped state by default. What should we do if we want the process to be executed in the background? The bg command does this.

Execute the script in 2 again, but after this execution, we immediately press CTRL + Z to pause it. Jobs view status:


The job marked as 3 is just added.

Execute bg % 3

View jobs again:


We can see that the job has been programmed by the Stopped status, and the command is followed by an additional &

5. kill the background work

Now there are more processes in the background. If some jobs do not need to be executed, it is always bad to put them in the background. In this case, you can use the kill command to delete the backend jobs. Command Format: Kill-signal % jobnumber

This is basically the same as kill killing a general process. The difference is that the number of the job is not the PID of the process.

Signal is the incoming Signal, which is commonly used as follows:

-L list available kill Signals

-1 re-read Configuration

-9 Force termination immediately (No cleanup work is available)

-15 terminate a job normally (there will be some subsequent cleanup work ).

To be safe, use the-9 parameter with caution.

Finally, give the following hints:

(1) In the VIM editing process, ctrl + z pause the work at hand and perform other operations, which are frequently used. For example, to prevent the modified Code from being accidentally synchronized to the production environment, we often need to check that the edited file has been placed in the ignore-list of rsync.

(2). Although the commands placed in the background are not interrupted by CTRL + C, all outputs are output to the screen by default. This should be solved through redirection. If the output content is not needed or ignored, you can simply redirect to the/dev/null device.

(3 ). if a script is executed for a long time, simply put it in the background, no matter whether the job may be interrupted due to TTY interruption (even in the background, the job will be interrupted ). In this case, you can use the nohup command to place the running script to the background of the system instead of the background of the current tty. The command is similar:

nohup php test.php 2>&1 

Now, enjoy the convenience of job Management.


How does one run tasks in the background through Linux bash?

Bash allows you to run one or more tasks later, and you can choose to suspend or restart any current task. To run a task in the background, add a & mark after the command line. Example:

Bash> tail-f/var/log/messages &
[1] 614

In this way, each background task is assigned a task ID and output in the console. You can run the command fg jobnumber to transfer the task from the background to the foreground, and jobnumber is the task ID you want to transfer to the foreground. Example:

Bash> fg 1

Input jobs at any bash prompt to obtain the list of tasks being executed.

At * how to delete a scheduled job

We recommend that you use Baidu to search for Kingsoft network security and use one-click repair to directly solve this problem. Easy to use.

Note:  will intercept the installation of Kingsoft network security. If you have , open the  Trojan firewall first and click the enabled button of the Process firewall to temporarily disable it.
(If the website security cannot be scanned, right-click the taskbar and exit the  tray)

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.