The 17th chapter, the procedure management and the SELinux research work Management (Job control)

Source: Internet
Author: User

Work management (Job control)

This job control is used in a bash environment, which means: " when we log into the system and get the bash shell, we perform multiple actions simultaneously under a single terminal interface ." 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! ^_^

What is Job management?

From the above instructions, you should understand: " in the conduct of work management, in fact, each job is the current bash subroutine, that is, the correlation between 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!"

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 at/etc/security/limits.conf (14th), in which case some users may only be able to work with one connection! So lo, you have to understand this mode of work management! In addition, this chapter will involve a lot of data flow redirection, so if you forget, be sure to go back to the 11th chapter BASH Shell to see Oh!

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

Then let's actually manage the work!

Management of Job Control

As mentioned earlier,Bash can only manage its own work and not manage other bash's work , so even if you are root, you will not be able to take someone else's bash job for him to run. In addition, the foreground and background, and then in the background of the working state can be divided into "pause" and "running (running)." What are the actual commands for job control? Let's talk at the bottom.

    • 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 &[18400  <== ~]# Tar:removing leading '/'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:

[Email protected] ~]# tar-zpcvf/tmp/etc.tar.gz/etc >/tmp/log.txt 2>&1 &[1] 8429[email Pro Tected] ~]

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! ^_^

Tips:
The job number is only relevant to your bash environment, but since he is a command-triggered boom, of course it must be a program, so you'll see that there's a job numbers with a PID too!

    • 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  : Besides listing the job number and the command string, the PID numbers are also listed;-R  : Lists only the work that is running on the background;-s  ~]# jobs-l[110314 Stopped                 vim ~/. bashrc[210833 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

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%It's dispensable! Example one: Working with jobs and then taking out the work: [[email protected]~]# jobs[1]-10314Stopped Vim ~/. bashrc[2]+10833StoppedFind/ -Print[[email protected]~]# FG <== The work of that + is removed 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]-StoppedFind/-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

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 RunFind/-perm +7000>/tmp/Text.txt, immediately throw to the background to pause! [[Email protected]~]#Find/-perm +7000>/tmp/text.txt# at this time, please press [Ctrl] immediately-Z Pause! [3]+ StoppedFind/-perm +7000>/tmp/Text.txt Example Two: Let the work in the background, and observe him!! [[Email protected]~]# jobs; BG%3; jobs[1]-Stopped Vim ~/. bashrc[2] StoppedFind/ -print[3]+ StoppedFind/-perm +7000>/tmp/text.txt[3]+Find/-perm +7000>/tmp/text.txt & <== with bg%3the situation! [1]+ Stopped Vim ~/. bashrc[2] StoppedFind/ -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

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, listing the currentKillWhat are the available signals (signal)? Signal: What kind of instruction does the delegate give to the next job? WithMans 7Signal:-1: Re-reading the configuration file of the parameter (similar to reload);-2: represented with input by keyboard [ctrl]-c the same action;-9: Forcibly delete a job immediately;- the: Terminates a work in a normal program manner. and9is 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] StoppedFind/ -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 find2Work's 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 and- theis the same! You can useKill-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!

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]   <==~]# 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 one. -seconds "program: [[email protected]~]# vim sleep500.SH#!/bin/Bash/bin/Sleep500s/bin/Echo "I have slept seconds."# 2throw in the background to run, and immediately unregister the system: [[email protected]~]#chmodA+x sleep500.SH[[Email protected]~]# nohup./sleep500.SH&[1]5074[[Email protected]~]# nohup:appending output to ' nohup.out ' <==will be informed of this information! [[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!

The 17th chapter, the procedure management and the SELinux research work Management (Job control)

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.