What is Job management?
"In the conduct of work management, in fact, each job is currently a subroutine of bash, which is related to each other." We can't manage Tty2 bash in the tty1 environment by job Control! "This concept you have to create first, after the introduction of the following example, you will clearly understand Luo!"
Given that we have only one terminal interface, it is called the foreground (foreground) in the context where the cue bytes can appear, so that you can put the background (background) to pause or run. It is important to note that when a job that is put into the background wants to run, he must not be able to interact with the user. For example, VIM is absolutely impossible to run in the background (running)! Because you didn't enter the data, he wouldn't run! And the job of putting the background is not to use [ctrl]+c to terminate!]
In summary, the restrictions that you must notice to perform the job control for bash are:
The programs triggered by these jobs must come from your shell's subroutines (just manage your own bash);
Foreground: You can control the work of the environment called foreground (foreground) with the placing of commands;
Background: You can run your own work, you cannot use [ctrl]+c to terminate him, you can use BG/FG to call the job;
The "Running" program in the background cannot wait for Terminal/shell input
Throw commands directly into the background of "Run" &
Tar-zpcf/tmp/etc.tar.gz/etc & [1] 8400 # The number in the brackets is thework number, which is related to bash control. # The next 8400 is the PID that works in the system. As the subsequent data flow is the tar run, # because we do not add data flow redirection, it will affect the screen! But it will not affect the operation of the foreground Oh!
|
Drop "current" work into the background "pause": [Ctrl]-z
[email protected] ~]# vi ~/.bashrc # in the general mode of VI, press [ctrl]-z these two keys [1]+ Stopped vim ~/.bashrc[[email prot Ected] ~]# <== successfully achieved the control of the future! [[email protected] ~]# 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 VI general mode, press [Ctrl] and z These two keys, the screen will appear [1], indicating that this is the first job, and that + represents the most recent job thrown into the background, and currently in the context of the default will be taken out of the job (FG this command)! And that Stopped represents the current state of the job. By default, work that is thrown into the background using [Ctrl]-z] is a "paused" state Oh!
Observe the current background work status: Jobs
[[em Ail protected] ~]# jobs [-lrs] options and Parameters:-L: In addition to listing the job number and command string, the PID numbers are listed at the same time;-R: Lists only the work that is running on the background;-S: Lists only the work that is paused (stop) in the background. example one: Observe the current All the work in bash, with the corresponding PID [[email protected] ~]# jobs-l [1]-10314 Stopped vim ~/.bashrc[2]+ 10833 Stopped find /-print |
If you want to know how much work is in the background, use the jobs command! In general, the direct release of jobs! However, if you still want to know the job number of the PID numbers, you can add-l this parameter! In the output of the information, such as the table above, see the +-number carefully! The + represents the default fetch work. So, "at the moment I have two jobs in the background, both are suspended, and if I just enter FG, then [2] will be taken to the foreground to deal with"!
Actually + represents the work number that was recently put on the background-represents the last working number that was placed in the background. And more than 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). Note that the percent is optional! 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 <== The work of the + by default, i.e. [2]. Immediately press [Ctrl]-z[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 able to take the background work to the foreground to deal with Luo! But the interesting thing is that the last one shows the result, and we will find that the + appears 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, and the last one that was put into the background will become the VI command action, so of course [1] will appear after the +! Know it! In addition, if the input "FG-" represents the number of the work number to take out, above is [2]-that work number!
Let the work in the background turn into a running state: BG
Example 1:1 running Find/-perm +7000 >/tmp/text.txt, immediately drop to the background to pause! [Email protected] ~]#Find/-perm +7000 >/tmp/text.txt# At this point, please press [ctrl]-z pause now! [3]+ Stopped Find/-perm +7000 >/tmp/text.txtExample 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]+StoppedFind/-perm +7000 >/tmp/text.txt[3]+ Find/-perm +7000 >/tmp/text.txt &<== with the bg%3 situation! [1]+ Stopped vim ~/.bashrc[2] Stopped Find/-print[3]-RunningFind/-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! Command line last party more than A & symbol Luo! On behalf of the work is launched in the background! ^_^
Work in the management context: Kill
[Email protected] ~]#kill-signal%jobnumber[Email protected] ~]#kill-lOptions and Parameters:-L: This is the lowercase of L, which lists the current signals (signal) that kill can use? Signal: What kind of instruction does the delegate give to the next job? With Man 7 signal know:-1: Re-read the configuration file of the parameter (similar to reload);-2: Represents the same action as input by the keyboard [Ctrl]-c;-9: Forcibly delete a job immediately;-15: Terminates a work in a normal program. 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 find 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-sigterm%1#-sigterm and 15 are the same! You can use Kill-l to check!
|
Pay special attention to this,-9 this signal is usually used to "force the deletion of an unhealthy job", and 15 is to end a job with a normal step (15 is the default value), which is not the same! In the above example, when I use VI, it will not produce a. filename.swp file? Then, 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. However, if you use the 9 signal, the VI work will be forcibly removed, so the. FILENAME.SWP will continue to exist in the file system. So you should be able to tell the difference a little bit?
In fact, kill's magical magic 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), and that Killall is the same usage! As for the signal you need to know at least 1, 9, 15 of the three signal of the meaning of a good. In addition, signal can use the signal name in addition to the numeric value! For example, the above example is one example! As for signal number and the name of the corresponding, hehe, use kill-l will know (l of lowercase)!
In addition, the number after the kill will be the default PID, if you want to manage bash's work control, you have to add a% number, this also has to pay special attention to it!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux-Work management (Job control), Jobs,fg,bg,kill