Job management under the shell [go]

Source: Internet
Author: User

Job Management

For example, when we landed bash, we wanted to copy files, search the data, compile on one side, and write the VI program on the side! Of course we can repeat the landing on the six-text interface in the terminal environment, but can it be achieved within a bash? Of course you can! Just use Job Control!

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!

Perhaps you will find it strange, since I can log in six terminal interface, why use job control? What a superfluous to take off my pants and fart! Don't forget, we can configure the number of connections that users can log in/etc/security/limits.conf, in which case some users may only work with one connection! So lo, you have to understand this mode of work management!

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" &

As mentioned above, we have only one bash environment, if we want to do multiple work at the same time, then we can throw some work directly into the background environment, so that we can continue to operate the foreground of the work! So how do you throw work into the background? The simplest way is to use the "&" thing! As a simple example, we want to make the entire/etc/backup/tmp/etc.tar.gz and don't want to wait, so we can do this:

[[email protected] ~]# tar-zpcf/tmp/etc.tar.gz/etc &[1] 8400  <== [job number] PID [[email protected] ~]# tar : Removing leading '/' from member names # The number in brackets is the work 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!

Take a closer look, after I enter a command, add a "&" at the end of the command to throw the command into the background, Bash gives the command a "job number", that [1]! To the back of the 8400 is the "PID" triggered by the command! And, interestingly enough, we can continue to do bash! It's not bad! But when did the work that was thrown into the background finish? What does it show when it's done? If you enter a few commands, the data suddenly appears:

[1]+  done                    tar-zpcf/tmp/etc.tar.gz/etc

On behalf of [1] This work has been completed (done), and the command of the work is followed by the string of command columns. You know that! In addition, this & Representative: "Throw the work into the background to run" Oh! Notice the word "Run"! In addition, the biggest benefit of this situation is: not afraid of being [ctrl]+c interrupted! In addition, throw work into the background to pay special attention to the flow of data Oh! Including the above information there is an error message that will cause my outlook to be affected. The prompt byte appears whenever you press [Enter]. But if I had just changed that order to:

[Email protected] ~]# TAR-ZPCVF/TMP/ETC.TAR.GZ/ETC &

What will happen? In the background of the command to run, if there is stdout and stderr, his data is still output to the screen above, so we will not be able to see the prompt byte, of course, will not be able to master the foreground work intact. At the same time because it is the background work of tar, at this time you how to press [Ctrl]+c also can not stop the screen is engaged in the colorful! So, the best thing to do is to use data flow redirection to send output data to a file. For example, I can do this:

Oh! As a result, the output of the information sent to him to/tmp/log.txt, of course, will not affect our prospects of the work. That said, you should be more aware of the importance of data flow redirection!

Drop "current" work into the background "pause": [Ctrl]-z

Think of a situation: if I am using VI, but found that I have a file do not know where to put, need to go to the bash environment to search, at this time whether to end VI? Oh! Of course you don't! Just temporarily throw VI to him in the background to wait. For example, the following cases:

[[Email protected] ~]# VI ~/.bashrc# in the general mode of VI, press [ctrl]-z these two keys [1]+  Stopped                 vim ~/.bashrc[[email protected] ~]#   <== 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
[[email 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 Pauses (stop) work in the background. Example one: Observe the current bash, all work, with the corresponding Pid[[email protected] ~]# jobs-l[1]-10314 Stopped                 vim ~/.bashrc[2]+ 10833 Stopped                 F IND/-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

Just mentioned is to throw the work into the background to run, then there is no background work to get the prospect to deal with it? Yes, it is! It's the FG (foreground)! For example, we want to take the work out of the above example to deal with:

[[email protected] ~]# FG%jobnumber options and Parameters:%jobnumber:jobnumber is the work number (number). Note that the percent is optional! Example one: Working with jobs and then taking out the work: [[email protected] ~]# jobs[1]-10314 Stopped                 vim ~/.bashrc[2]+ 10833 Stopped                 Find/-prin T[[email protected] ~]# FG      <== The work of that + is removed by default, i.e. [2]. Immediately press [Ctrl]-z[[email protected] ~]# FG%1   <== The work number that was removed directly! then press [ctrl]-z[[email protected] ~]# jobs[1]+  Stopped                 vim ~/.bashrc[2]-  Stopped                 Find/-prin

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

We just mentioned that [ctrl]-z can put the current job under the background to "pause", so how to get a job under the background "Run"? We can test it in the bottom case! Watch out! The test below is going to go a little faster!

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.txt Example two: Get the job done in the background and watch 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.txt &  <== with 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! 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

Just so we can get a job that's already in the background and get the job done with FG, so if you want to remove the job directly? Or do you restart the job? At this point you need to give the job a signal (signal) and let him know what to do. At this point, kill this command will come in handy!

[[email protected] ~]# kill-signal%jobnumber[[email protected] ~]# kill-l options and Parameters:-L  : This is the lowercase of L, which lists the signals currently available for Kill (SIG NAL) What is there? 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: With normal program Termination of a work. 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]   killed                  find/-print# in a few seconds you'll be given jobs again, and you'll see that number 2nd is gone! 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 with-15 is the same Of 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 need to pay special attention to it.

Offline management issues

It is important to note that the "background" we refer to in our work management refers to a situation where the crtl]-c interrupt can be avoided in the terminal mode, not on the background of the system! So, the background of work management is still related to the terminal! In this case, if you are connected to your Linux host remotely, and will work in the way of & to the background, ask, in the case of the work is not over, you offline, the work will continue to do? The answer is "no"! Will not go on, but be interrupted.

So what? If my work is going to take a long time and I can't put it under the background, how do I deal with it? First, you can refer to the previous chapter of the AT to handle it! Because at is to place work on the system background and not on the terminal. If you do not want to use at, then you can also try to use the Nohup command to deal with Oh! This nohup allows you to continue your work after you have disconnected or logged out of the system. His grammar is a bit like this:

[[email protected] ~]# nohup [commands and parameters]   <== working in the foreground of the terminal [[email protected] ~]# nohup [commands and Parameters] & <== working in the terminal background

Good enough simple command! It is important to note that NOHUP does not support bash built-in commands, so your command must be an external command. Let's try to play the task below!

# 1. First edit a program that will "sleep 500 seconds": [[email protected] ~]# vim sleep500.sh#!/bin/bash/bin/sleep 500s/bin/echo "I have slept seconds." # 2. Throw it into the background to run, and immediately unregister the system: [[email protected] ~]# chmod a+x sleep500.sh[[email protected] ~]# nohup./sleep500.sh &[1] 5074[[e Mail protected] ~]# nohup:appending output to ' nohup.out ' <== will tell this message! [[Email protected] ~]# exit

If you land again, then use Pstree to check your program, you will find that sleep500.sh is still in operation Oh! And will not be interrupted! Do you understand the meaning? Since our program will eventually output a message, but the nohup and the terminal is actually irrelevant, so the output of this information will be directed to the "~/nohup.out", so you will see the above command, when you enter Nohup, will appear after the message ROM.

If you want to keep the background work running after you log off, then using Nohup with & is a good running situation! can refer to look!

Transfer from http://vbird.dic.ksu.edu.tw/linux_basic/0440processcontrol_2.php

Job management under the shell [go]

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.