Linux Job Management

Source: Internet
Author: User

Linux Job Management

Before introducing job management, you must first know that job management is used in the bash environment. That is to say, "After logging on to the system to obtain the bash shell, you can manage multiple jobs at the same time on a single terminal interface ". In this way, we should know that when you manage jobsEach job is the current bash subroutineThat is, they are associated with each other. We cannot manage the bash of tty2 in the tty1 environment by job management.

Directly put the command in the background to "execute &

In the bash environment, the foreground refers to jobs that you can control. Background: you cannot directly control a job that can be run in the memory unless the job is called using commands such as bg/fg.

Example:

[Root @ redflag ~] # Tar-zpcf/tmp/etc.tar.gz/etc & [1] 24874 <= [job number] PID [root @ redflag ~] # <= Can continue to work, not affected.

Here, job1 continues working in the background. You can continue operating linux on the foreground. After a while, the data will suddenly appear:

[1] + Done tar-zpcf/tmp/etc.tar.gz/etc # This indicates that the [1] job has been completed

The biggest benefit of this command is that it is not afraid of being interrupted by the "[Ctrl] + c" command.
A problem occurs here, if the above command is

[root@redflag ~]# tar –zpcvf /tmp/etc.tar.gz /etc &

In this case, if stdout and stderr are run in the background, the data is output to the screen, so we cannot see the prompt, and the foreground job cannot be properly controlled. Therefore, it is best to use data stream redirection to transmit the output data to a file. We can do this:

 [root@redflag ~]# tar –zpcvf /tmp/etc.tar.gz /etc > tpm/log.txt 2>&1 &

In this way, data is transmitted to/tmp/log.txt, which naturally does not affect the foreground job.

Put the current job in the background and pause: [crtl] + Z

If you are running vi, but you want to find a file, you need to search in the bash environment. Now you can leave vi temporarily

[Root @ redflag ~] # Vi ~ /. Bashrc # In General vi Mode, press ctrl + z [1] + Stopped/usr/bin/vim ~ /. Bashrc # (+) indicates the default job in the background [root @ redflag ~] # <== Obtain control of the foreground
Observe the current background job status: jobs
[Root @ redflag ~] # Jobs [-lrs] parameter:-l: Besides listing job numbers, PID-r is also listed: only jobs running in the background-s is listed: only jobs paused in the background are listed

Example:

 [rot@redflag ~]# jobs –l [1]+ 24988 Stopped /usr/bin/vim ~/.bashrc [2]- 25006 Stopped /usr/bin/vim ~/.bash_history

Generally, run jobs directly. In the above output, there is a (+-) number. (+) Indicates the default job. Therefore, "Currently, I have two jobs in the background, both of which are paused. If I only input fg, [1] will be taken to the foreground for processing"

Take the background job to the foreground for processing, fg
 [root@redflag ~]# fg %jobnumber

Example:

[Root @ redflag ~] # Jobs [1] + 24988 Stopped/usr/bin/vim ~ /. Bashrc [2]-25006 Stopped/usr/bin/vim ~ /. Bash_history [root @ redflag ~] # Fg <= the default output + job, that is, [1] [root @ redflag ~] # Fg % 2 <= specifies the job number retrieved directly
Job running in the background: bg

Previously, jobs were "paused" in the background, while bg implemented a job to run in the background.

[Root @ redflag ~] # Find/-perm + 7000 # immediately press ctrl + z to pause [1] + stopped find/-perm + 7000 [root @ redflag ~] # [Root @ redflag ~] # Jobs; bg % 1; jobs [1] + Stopped find/-perm + 7000 [1] + find/-perm + 7000 & [1] + Running find/-perm + 7000 &

In this case, Stopped becomes Running, and the command line has an & symbol, indicating that the job is started in the background.
Note: during the operation from 1 to 5, "% jobnumber" can be omitted and jobnumber can be directly added.

Manage background jobs: kill
[Root @ redflag ~] # Kill-signal % jobnumber [root @ redflag ~] # Kill-l parameter:-l: list signals that can be used by the current kill (Note: lower case L)-1: re-read the setting file of the parameter (similar to reload) --- the corresponding signal is SIGHUP-2, indicating the same action as pressing ctrl + c on the keyboard --- the corresponding signal is SIGINT-9: immediately force delete a job-the corresponding signal is SIGKILL-15: terminate a job in Normal Mode-the corresponding signal is SIGTERM [root @ redflag ~] # Jobs [1] + Stopped vim bashrc [root @ redflag ~] # Kill-9% 1 # forcibly Delete [1] + vim bashrc [root @ redflag ~] # Kill-SIGTERM % 1 #-SIGTERM is the same as 15

Note the difference between terminating a job in a normal and forced way: for example, a file is generated when you use vi to edit the file. filename. swp file. If the job ends normally. filename. the swp file is automatically deleted. Instead, the job is ended in a forced way. filename. the swp file continues to exist in the file system.
Note: kill can help us send signals to a job (% jobnumber) or a PID (entering numbers directly), that is, adding numbers directly after kill is different from adding %.

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.