[Linux] Job Management in Bash

Source: Internet
Author: User

??I was not prepared to write this blog, because task management (job management) is very common, so that there is no need to write such a thing. But think about it, or record it, maybe someone will use it.

I do not know if you have encountered such a situation, when you open vim in a cheerful mood, write code written to the busy, operating mm or product mm panting run over: "Hurriedly help me Run XX data, one will do PPT to use." But do not want to directly close the current vim, and some systems, and can not open a new TTY (such as set the Maxlogins parameter) to perform these tasks, what can I do? This actually involves job management.

So what is task management? As the name implies, it means adding, removing, processing, and so on, multiple jobs in the same TTY in bash. Since each of the work in bash is actually a subprocess of the current bash, job management is fundamentally about managing the process . Note that the word "same TTY" is used in the above statement, because we are unable to manage the job in Tty2 through tty1 job management. For example, we put vim into the background by CTRL + Z at the Tty3 Terminal, and in the current TTY, the list of background tasks seen through the jobs command looks like this:

And then log in to the new TTY, viewed through the jobs command, and found no jobs in the background:

Job Control

1. Throw the job into the background by CTRL + Z

Just now we have seen, in the process of vim editing, through the Ctrl + Z shortcut key way, you can put the current vim into the background pause, and the TTY terminal will output the following words prompt:

[1]+  Stopped                 vim

where [1] refers to the job number, after which the work will be placed in the foreground, or kill the background process will be used.

Stopped represents the state of the current process (in fact, jobs placed in the background by CTRL + Z are Stopped states), and Vim refers to the job being placed in the background. Wait, what does that plus + mean? If we put 2 jobs into the background, we can view the jobs list in the current background through the jobs command, and the output is as follows:

Found not only the + number, but also-number. In fact, the + number refers to the input FG command, the default will be identified as the + number of the job to take to the foreground to execute,-the number is the + job is FG after the spare tire.

2. Drop the command in the background execution &

Under Bash, there are often time-consuming tasks, such as reading a log script, which can take more than 10 minutes to complete, and can't always stare at it? At this point, the best way to add & after the command, and put it in the background to execute

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

<? PHP $i = 0;  while ($i+ <) {    echo$i;     Sleep (1);}

Then throw it into the background to execute:

We see that even in the background, the output of the program is output to bash's standard output, even if you press CTRL + C. This can be resolved by redirection :

2>&1 >>test.log &

View the status of jobs through the jobs command:

Almost the same as just now, except this time thrown into the background is the Running state. After the task has finished executing, the screen will output the following words:

Indicates that the current work has been completed.

3. Get the backstage work to the front desk to process FG (%jobnumber)

The previous only talked about how to put work into the background and how to view the background jobs status. Now the data run out, operation mm go, continue to open the background of the Vim code bar. This needs to use the FG command.

Command format: FG [%jobnumber]. where the contents of [] can be saved, the default is to check out the job labeled +. If you want to remove the specified job, you need to pass in the job number:

FG%1

You can remove the job with the number 1

4. Make the work in the background state into a running BG (% jobnumber)

We know that through Ctrl + Z placed in the background of the job, the default is stopped state, if you want to let the process in the background, what should be done? The BG command is doing this.

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

  

The job labeled 3 is the one we just joined.

Perform BG%3

Check jobs again:

  

As you can see, the job has been programmed running state by the stopped state, and the command is followed by A &

5. Killing background work kill

now, the background of the process more up, if some job does not need to execute, put in the background is always bad.   At this point, you can delete the background jobs by using the KILL command. Command format:kill–signal%jobnumber

This is basically no different than kill kills the general process, except that this is the job's number, not the PID of the process.

Signal is the incoming signal, commonly used are:

-l LIST the available signals for kill

1 Re-read configuration

-9 immediate forced termination (no cleanup work)

-15 normal termination of a job (there will be some follow-up cleanup work).

For insurance purposes, use the 9 parameter sparingly.

Finally, give several hint:

(1). Vim during the editing process Ctrl + Z pauses the work at hand, doing some other processing, is often used by the operation. For example: To prevent the modified code from being accidentally synced to the line, we often need the check edit file to be placed in the ignore-list of rsync.

(2). The command, which is placed in the background by &, will not be interrupted by CTRL + C, but all output by default will be output to the screen, which should be resolved by redirection. If the output is not required or ignored, you can simply redirect to the/dev/null device.

(3). If a script executes for a longer period of time, simply putting it into the background can be interrupted by a TTY outage (even in the background, where the TTY is interrupted, the job will be interrupted). You can then place the run script in the background of the system instead of the current TTY through the Nohup command, similar to the following:

2>&1  &

Now, enjoy the convenience of job management.

[Linux] Job Management in Bash

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.