Linux-Work management (Job control), Jobs,fg,bg,kill

Source: Internet
Author: User
Tags ming

What is Job management?

"In the conduct of work management, the fact that every job is a child of bash at the moment, i.e., is related to each other." We can't manage Tty2 bash in the tty1 environment by job Control! "This concept should be created first, perhaps after the introduction of the example." You'll know what's going on.

Because if we only have a terminal interface, the environment that allows you to operate in the presence of cue bytes is called the foreground (foreground). As with any other job, you can put it in the background (background) to pause or execute.

It is important to note that when the work that is put into the background wants to be executed, he must not be able to interact with the user. For example, VIM is absolutely impossible to perform in the background (running)! Because you didn't enter the data, he wouldn't run. And the job of putting the background is not able to use [ctrl]+c to terminate!]

In summary, the restrictions that you need to be aware of in order to bash job control are:

The programs triggered by these jobs must come from your shell's subroutines (just manage your own bash);
Foreground: You are able to control this environment called foreground work (foreground) with the command being issued.
Background: Work that can be performed on its own. You cannot use [ctrl]+c to terminate him, you can use BG/FG to call the job.
The "Execute" program in the background cannot wait for Terminal/shell input


Drop commands directly into the background "execute" &

Tar-zpcf/tmp/etc.tar.gz/etc & [1] 8400  # The number in the brackets is thework number, which is related to bash control.

# Maybe 8400 is the PID that works in the system. The data that may appear is the data stream that the tar executes, # because we do not add the data stream redirect, so it will affect the screen! It just doesn't affect the foreground of the operation Oh!


Drop the "Now" job into the background "pause": [Ctrl]-z

VI ~/.BASHRC # in the general mode of VI. Press [ctrl]-z These two keys [1]+  Stopped                 vim ~/.bashrc[[email protected] ~]#   <== successfully gained the control of the foreground!

Find/-print .... (output omitted) .... # At this point the screen will be very busy!

Because all the file names are displayed on the screen. Please press [ctrl]-z pause [2]+ Stopped Find/-print


In the general mode of VI. Press [Ctrl] and Z for the two keys. [1] appears on the screen. Indicates that this is the first job. And that + represents a recent job thrown into the background. And the work that will be taken by default in the background (in relation to the FG command)! And that Stopped represents the state of the job at the moment.

By default, working with [ctrl]-z thrown into the background is a "paused" state!

Look at the current state of background work: jobs

 [[em Ail protected] ~]# jobs [-lrs]   options and Parameters:-L: The number of the PID is listed at the same time, in addition to listing job numbers and command strings. -R: Lists only the work being run on the background. -S: Lists only the work that is paused (stop) in the background.  

example one: Observe the current Bash one of them. All the work. With the corresponding PID [[email protected] ~]# jobs-l [1]-10314 Stopped vim ~/.bashrc[2]+ 10833 Stopped find /-print


Suppose you want to know how much work is in the background right now. Just use the jobs command!

Generally speaking. Just release Jobs! Just, let's say you want to know the PID number of the job, plus the-l parameter! In the output of the information therein. For example, the above table, carefully see the +-Number Oh!

The + represents the default fetch work. So say: "Right now I have two jobs in the background." Both of the jobs are suspended. And assuming I enter FG only, then that [2] will be taken to the foreground for processing "!

In fact + represents a work number that has been put in the background recently, representing the last working number that was placed in the background. And beyond the last third job. There will be no +/-symbol exists!

Get background work to the foreground: FG

[Email protected] ~]#FG%jobnumberoptions and Parameters:%jobnumber:jobnumber is the work number (number). Attention. That percent is dispensable. Example One: Work is first observed, and then the work is taken out:[Email protected] ~]#Jobs[1]-10314 Stopped vim ~/.bashrc[2]+ 10833 Stopped Find/-print[[email protected] ~]#FG   <== default to take out that + job. i.e. [2]. Press [Ctrl]-z] immediately .[Email protected] ~]#FG%1   <== directly rules out that work number! Then press [Ctrl]-z[Email protected] ~]#Jobs[1]+Stopped vim ~/.bashrc[2]-Stopped Find/-print

The FG command will be the background work to get the foreground to deal with Luo! Just the more interesting is the final display of the results, we will find + out now after the first work! How could this be? This is because you just took the first job to the foreground with FG%1 and put it back in the background, when the last one that was put into the background becomes the VI command action. So of course [1] will appear after the +! Understand. Other than that. Suppose that the input "FG-" represents the work number that will be-the number is taken out. Above is [2]-that work number!


Let the state of work in the background turn into execution: BG

example 1:1 executing find/-perm +7000 >/tmp/text.txt, immediately throw to the background to pause! [Email protected] ~]#Find/-perm +7000 >/tmp/text.txtAt this time Please press [ctrl]-z pause!]

[3]+ Stopped Find/-perm +7000 >/tmp/text.txt example two: Let the work take place in the background, and observe him!. [[email protected] ~]# jobs; BG%3; Jobs [1]-Stopped vim ~/.bashrc[2] Stopped Find/-print[3]+ stopped Find/-perm +7000 >/tmp/text.txt[3]+ Find/-perm +7000 >/tmp/text.t XT & <== with the bg%3 situation!

[1]+ Stopped vim ~/.bashrc[2] Stopped Find/-print[3]- Running Find/-perm +7000 >/tmp/text.txt &


See where there's a difference? Whirring That's right! That is the State column ~ to become Running by stopping!

See the difference point, hey! The command line ends with a & symbol Luo. On behalf of the work was launched in the background. ^_^

Management background in which work: Kill

[Email protected] ~]#kill-signal%jobnumber[Email protected] ~]#kill-lOptions and Parameters:-L: This is the lowercase of l, which lists what signals (signal) can be used for kill now? Signal: What kind of instruction does the delegate give to the next job?  With Man 7 signal know:-1: Once again read the configuration file (similar to reload).  -2: Represents the same action as the input [ctrl]-c] by the keyboard. -9: Immediately force deletion of a job;-15: Terminates a work in a normal program manner. And-9 are not the same. Example one: Identify background work in the current bash environment and "Force delete" the work. [Email protected] ~]#Jobs[1]+ Stopped vim ~/.bashrc[2] Stopped Find/-print[[email protected] ~]#kill-9%2; Jobs[1]+ Stopped vim ~/.bashrc[2]killedFind/-print# In a few seconds you'll be given jobs again, and you'll see that number 2nd is missing. Because it was removed!

Example: Identify background work in the current bash environment and "gracefully terminate" the work. [email protected] ~]# jobs[1]+ Stopped vim ~/.bashrc[[email protected] ~]# Kill-sigte RM%1#-sigterm and-15 are the same. You can use Kill-l to check!


Pay special attention. -9 This signal is generally used when "forced to delete an unhealthy job", 15 is the normal step to end a work (15 is the default value), not the same yo! In the example above, I use VI. Does it not produce a. filename.swp file? So. When using the-15 signal, VI will attempt to end the work of the VI in a normal step, so. FILENAME.SWP will be removed actively. But if you use the-9 signal. Since the vi work will be forcibly removed, the. FILENAME.SWP will continue to exist in the file system. So you should be able to tell a little bit about it?

In fact, the magical effect of Kill is very infinite! With Signal's detailed information (using Man 7 signal to access relevant data), you can effectively manage your work and programs (process), in addition. That Killall is the same way of using it. You need to know at least 1, 9, and 15 of the three signal of the signal you use regularly. In addition, signal is able to use the signal name in addition to being represented by a numeric value Oh! For example, the example above is a sample!

Signal number with the name of the corresponding, hehe, use kill-l will know (l of lowercase)!

In addition, the number followed by Kill will be PID by default. Suppose you want to manage bash's work control. We have to add a percentage of the number, so we have to pay special attention to it!

Linux-Work management (Job control), Jobs,fg,bg,kill

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.