Shell Job Management)

Source: Internet
Author: User
Job Management

For example, after logging on to bash, we want to copy files, search data, compile data, and write vi programs! Of course, we can log on to the terminal environment of the six text interfaces again. However, can we achieve this in a bash? Of course! Job control is used!

In the behavior of Job Management, every job is the current bash subroutine, that is, there is a correlation between them. We cannot manage the bash of tty2 in the tty1 environment in the form of job control!

Maybe you may find it strange. Since I can log on to six terminal interfaces, why should I use job control? Just put off your pants and fart! Do not forget. We can go to/etc/security/limits. in conf, configure the number of connections that users can log on to at the same time. In this case, some users may only work with one connection! So Luo, you have to understand this kind of work management model!

Assume that we only have one terminal interface. Therefore, when a prompt byte is displayed, the environment for your operation is called the foreground (foreground). By other work, you can put it in the background) pause or run. It should be noted that when a job that is placed in the background wants to run, it must not be able to interact with the user. For example, vim cannot run (running) in the background! He won't run because you didn't input the data! In addition, you cannot use [ctrl] + c to terminate the work in the background 』!

In short, the following restrictions must be noted for bash job control:

  • The program triggered by these jobs must come from your shell subroutine (only manage your own bash );
  • Foreground: The environment in which you can control and issue commands is called the foreground work );
  • Background: You can run the job on your own. You cannot use [ctrl] + c to terminate the job. You can use bg/fg to call the job;
  • In the background, the program "run" cannot wait for the input (input) of terminal/shell)

 

Directly drop the command to the background and "run &

As mentioned above, if we only have one bash environment and want to perform multiple jobs at the same time, we can directly drop some jobs into the background environment, so that we can continue to work in the future! How can I leave my work to the background? The simplest way is to use! For example, if you want to back up/etc/to/tmp/etc.tar.gz and do not want to wait, you can do this:

[Root @ www ~] # Tar-zpcf/tmp/etc.tar.gz/etc & [1] 8400 <= [job number] PID [root @ www ~] # Tar: Removing leading '/' from member names # the number in brackets is the job number, which is related to bash control. # The next 8400 is the PID in the system. As for the subsequent data, it is the data stream running in tar. # because we have not added data stream redirection, this will affect the picture! However, it will not affect the foreground operations!

After entering a command, I add "&" at the end of the command to drop the command to the background, bash will give this command a "job number", which is [1! The last 8400 is the "PID" triggered by this command! What's interesting is that we can continue to operate bash! It's good! However, when will the work that is thrown into the background be completed? What will be displayed upon completion? If the data suddenly appears after you enter several commands:

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

It indicates that [1] The job has been completed (Done), and the command for this job is connected to the subsequent command column. Now, let's get to know! In addition, this & represents: "throwing your work to the background to run! Notice the word "run! In addition, the biggest benefit of this situation is that you are not afraid of being interrupted by [ctrl] + c! In addition, pay special attention to the data flow when throwing your work to the background! The above information contains an error message, which affects my prospects. Even if you press [enter], the prompt byte is displayed. However, if I change the command:

[root@www ~]# tar -zpcvf /tmp/etc.tar.gz /etc &

What will happen? If stdout and stderr are run in the background, their data is still output to the screen, so we cannot see the prompt byte, of course, you will not be able to grasp the prospects well. At the same time, because it is the tar of the background work, how can you press [ctrl] + c to stop the screen being made green! Therefore, the best practice is to use data stream redirection to transmit output data to a file. For example, I can do this:

[root@www ~]# tar -zpcvf /tmp/etc.tar.gz /etc > /tmp/log.txt 2>&1 &[1] 8429[root@www ~]# 

Haha! As a result, the output information is transmitted to/tmp/log.txt, which of course will not affect our job prospects. In this case, you should be clear about the importance of data stream redirection!

 

Throw the current job to the background and pause: [ctrl]-z

Think of a situation: If I am using vi, but find that I have a file that does not know where to put it, I need to search in the bash environment. Do I want to end vi at this time? Haha! Of course not required! You only need to temporarily throw vi to the background and wait. For example:

[Root @ www ~] # Vi ~ /. Bashrc # In normal vi Mode, press the [ctrl]-z buttons [1] + Stopped vim ~ /. Bashrc [root @ www ~] # <== Successfully achieved the right to control the future! [Root @ www ~] # Find/-print... (output omitted)... # the screen will be very busy! Because all file names are displayed on the screen. Please press [ctrl]-z to pause [2] + Stopped find/-print

In normal vi Mode, press the [ctrl] and z buttons, and [1] will appear on the screen, indicating that this is the first job, that + represents the last job that has been thrown into the background and is currently used by default in the background (related to the command fg )! The Stopped indicates the current working status. By default, [ctrl]-z is used to throw the job to the background and the job is paused!

 

Observe the current background working status: jobs
[Root @ www ~] # Jobs [-lrs] Options and parameters:-l: In addition to listing the job number and command string, the PID number is also listed;-r: only list jobs in the background run; -s: only list jobs that are being paused in the background. Example 1: Observe all the work in the current bash, and the corresponding PID [root @ www ~] # Job-l [1]-10314 Stopped vim ~ /. Bashrc [2] + 10833 Stopped find/-print

If you want to know how much work is currently under the background, use the "jobs" command! Generally, you can directly issue jobs! However, if you want to know the PID number of the job number, you can add the-l parameter! In the output information, for example, in the table above, you can see the +-number carefully! That + indicates the default access work. Therefore, "At present, I have two jobs in the background, both of which are paused. If I only input fg, then [2] will be processed in the foreground 』!

In fact, "+" indicates the work number recently placed on the background. "-" indicates the last and second work number placed on the background. After the third and last jobs, there will be no +/-symbols!

 

Apply background work to the foreground: fg

I just mentioned that I threw my work to the background to run it. Can I handle the background work in the foreground? Yes! That's the fg (foreground! For example, when we want to take out the work in the above sample for processing:

[Root @ www ~] # Fg % jobnumber option and parameter: % jobnumber: jobnumber is a work number (number ). Note that % is dispensable! Example 1: first observe the work with jobs, and then extract the work: [root @ www ~] # Jobs [1]-10314 Stopped vim ~ /. Bashrc [2] + 10833 Stopped find/-print [root @ www ~] # Fg <= The Work of the + is retrieved by default, that is, [2]. Immediately press [ctrl]-z [root @ www ~] # Fg % 1 <== specifies the work number retrieved directly! Then press [ctrl]-z [root @ www ~]. # Jobs [1] + Stopped vim ~ /. Bashrc [2]-Stopped find/-prin

After running the fg command, you can get the background work to the foreground to handle it! But what is interesting is the result of the last display. We will find that + appears after the first job! Why? This is because you just used fg % 1 to capture the foreground and then put it back into the background. At this time, the last one that is placed in the background will become the command action of vi, so of course, + will appear later than [1! Understanding! In addition, if you enter "fg-", it means you can take out the work number of the-number, which is [2]-The work number!

 

Change the working background to running: bg

As we just mentioned, the [ctrl]-z can throw the current job to the background and "pause". How can we make a job "Run" under the background? We can test it in the following case! Note! The tests below will be faster!

Example 1: After you run find/-perm + 7000>/tmp/text.txt, immediately throw it to the background to pause! [Root @ www ~] # Find/-perm + 7000>/tmp/text.txt # at this time, immediately press [ctrl]-z to pause! [3] + Stopped find/-perm + 7000>/tmp/text.txt Example 2: let the job proceed in the background and observe it !! [Root @ www ~] # 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 & <== use bg % 3! [1] + Stopped vim ~ /. Bashrc [2] Stopped find/-print [3]-Running find/-perm + 7000>/tmp/text.txt &

Are there any differences? Call! That's right! That is the Status column ~ By Stopping, it becomes Running! You can see the difference! An & symbol is added at the end of the command column! It indicates that the job has been started in the background! Pai_^

 

Manage jobs in the background: kill

Just now we can let a job that has already been in the background continue to work, but also let the job get the prospect of fg, then if you want to remove the job directly? Or restart the job? At this time, you need to give the job a signal to let him know how to do it! In this case, kill is useful!

[Root @ www ~] # Kill-signal % jobnumber [root @ www ~] # Kill-l option and parameter:-l: the lower case of L. Which of the following signals can be used by kill? Signal: indicates what kind of instruction should be given to the subsequent job! Man 7 signal:-1: re-read the parameter configuration file (similar to reload);-2: represents the same action as the keyboard input [ctrl]-c; -9: Force delete a job immediately.-15: terminate a job as a normal program. It is different from-9. Example 1: Find the background work in the current bash environment and "Force Delete" the work 』. [Root @ www ~] # Jobs [1] + Stopped vim ~ /. Bashrc [2] Stopped find/-print [root @ www ~] # Kill-9% 2; jobs [1] + Stopped vim ~ /. Bashrc [2] Killed find/-print # After you release jobs again in a few seconds, you will find that job 2 is gone! Because it was removed! Example: Find the background work in the current bash environment and "Terminate" The work normally. [Root @ www ~] # Jobs [1] + Stopped vim ~ /. Bashrc [root @ www ~] # Kill-SIGTERM % 1 #-SIGTERM is the same as-15! You can use kill-l for details!

Note that the-9 signal is usually used to forcibly delete an abnormal job, and-15 ends a job in a normal step (15 is also the default value ), they are not the same! In the above example, when I use vi, isn't a. filename. swp file generated? Then, when the signal-15 is used, vi will try to end the work of the vi with a normal step, so. filename. swp will be automatically removed. However, if the signal-9 is used, the vi work will be forcibly removed. Therefore,. filename. swp will continue to exist in the file system. So you can tell a little bit about it?

In fact, the use of kill is endless! It works with the information detailed by signal (use man 7 signal to view relevant data) to allow you to effectively manage processes. In addition, the killall is also used in the same way! For Commonly Used signal, you must understand at least the meaning of the three signal values 1, 9, and 15. In addition to numerical values, signal can also use the signal name! For example, the example 2 above is an example! As for the correspondence between the signal number and the name, you can use kill-l (lower case of L )!

In addition, the number followed by kill Will be PID by default. If you want to manage bash's work control, you have to add % of the number, so pay special attention to this.

 

Offline Management Problems

Note that the background we mentioned in work management refers to a situation where [crtl]-c interruptions can be avoided in terminal mode, it's not the background of the system! Therefore, the background of work management is still related to the terminal! In this case, if you connect to your Linux host remotely and put the work in the & mode to the background, could you tell me, if your work is offline, will the work continue? The answer is no 』! It will not continue, but will be interrupted.

What should we do? What should I do if my work takes a long time and I cannot place it under the background? First, you can refer to at in the previous chapter to process it! Because at is to place the work in the system background, and has nothing to do with the terminal. If you do not want to use at, you can also try to use the nohup command to handle it! This nohup allows you to continue working after you log out of the system or go offline. His syntax is a bit like this:

[Root @ www ~] # Nohup [commands and parameters] <= working in the terminal foreground [root @ www ~] # Nohup [commands and parameters] & <== working in the terminal background

There are simple enough commands! Note that nohup does not support bash built-in commands. Therefore, your commands must be external commands. Let's try the following task!

#1. edit a program that will "sleep for 500 seconds": [root @ www ~] # Vim sleep500.sh #! /Bin/bash/bin/sleep 500 s/bin/echo "I have slept 500 seconds. "#2. run it in the background and immediately log out of the system: [root @ www ~] # Chmod a + x sleep500.sh [root @ www ~] # Nohup./sleep500.sh & [1] 5074 [root @ www ~] # Nohup: appending output to 'nohup. out' <= indicates this message! [Root @ www ~] # Exit

If you log on again and use pstree to check your program, you will find that sleep500.sh is still running! It will not be interrupted! Do you understand this? Because our program will output a message at the end, but nohup has nothing to do with the terminal, the output of this information will be directed 『~ /Nohup. out ", so you will see the prompt information in the above command. When you enter nohup.

If you want to enable the background work to continue running after you log out, use nohup with a good running situation! For more information, see!

 

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

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.