Before you introduce job management, you need to know that job management is used in the bash environment. This means "when the login system is acquired bash shell , under a single terminal interface, multiple jobs are managed simultaneously". It should be understood that when you manage a job, each job is actually bash the current subroutine , which is related to each other. We cannot manage it in a way that is managed by tty1 the tty2 environment bash .
Put the command directly in the background "execute" &
In bash the environment, the foreground refers to: you can control the work. Background refers to a job that can run itself in memory, and you cannot control it directly unless bg/fg the job is called out with such commands.
Example:
[root@redflag ~]# tar –zpcf /tmp/etc.tar.gz /etc &[124874 PID[root@redflag ~]# <==可以继续工作,不受影响。
job1continue working in the background, you can continue to operate in the foreground linux . After a while, this data will suddenly appear:
[1] + Done tar –zpcf /tmp/etc.tar.gz /etc #这是表示[1]作业已经完成
The greatest benefit of this command is that it is not afraid “[Ctrl]+c” to be interrupted by this interrupt command.
There is a problem here, if the above command is
[root@redflag ~]# tar –zpcvf /tmp/etc.tar.gz /etc &
At this point in the background execution of the command, if there is stdout and stderr when its data is output to the screen, so we can not see the prompt, it will not be able to control the foreground job intact. Therefore, it is best to use data flow redirection to pass 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, the data is transmitted to the /tmp/log.txt medium, which naturally does not affect the foreground job.
Put the "current" job in the background "pause": [Crtl]+z
If you are working on vi it, but you want to find a file, you need to bash search the environment. You can leave at this moment.vi
[root@redflag ~]# vi ~/.bashrc # 在vi 的一般模式下,按ctrl+z[1]+ Stopped /usr/bin/vim ~/.bashrc #(+)表示当前在后台下默认的作业[root@redflag ~]# <== 获取了前台的控制权
Watch the current background job status: Jobs
[[email protected] ~]# jobs [-lrs] 参数: -l:除了列出作业号之外,同时列出PID -r:仅列出正在后台运行的作业 -s:仅列出正在后台暂停的作业
Example:
[rot@redflag ~]# jobs –l [124988 Stopped /usr/bin/vim ~/.bashrc [225006 Stopped /usr/bin/vim ~/.bash_history
In general, you can do it directly jobs . In the above output, there is the ( + - ) number. ( + ) represents the default job. So "At present I have two jobs in the background, two jobs are suspended, if I enter only fg , then [1] will be taken to the foreground processing"
Get the background job to the foreground, FG
[root@redflag ~]# fg %jobnumber
Example:
[root@redflag ~]# jobs [124988 Stopped /usr/bin/vim ~/.bashrc [225006 Stopped /usr/bin/vim ~/.bash_history [root@redflag ~]# fg <==默认取出+的作业,即[1] [root@redflag ~]# fg %2 <==直接规定取出的作业号码
Jobs running in the background: BG
Previously, the job was put in the background to "pause" while bg implementing a job running in the background.
[Root @redflag ~]# Find/-perm +7000 #此时, please press CTRL + Z to pause immediately [1 ]+ stopped find/-perm +7000 [Root @redf Lag ~]# [Root @redflag ~]# jobs; BG%1; Jobs [1 ]+ stopped Find/-perm +
7000 [
1 ]+ Find/-perm +
7000 & [
1 ]+
running Find/-perm +
7000 &
This Stopped becomes the Running end of the command line with a & symbol that indicates that the job started in the background.
Note: The 1-5 operation “%jobnumber” can be “%” omitted directly added jobnumber .
Manage Background jobs: Kill
[Root@redflag~]# kill–signal%jobnumber[Root@redflag~]# kill–lParameter:-L: Lists the currentKillAble to use the signal (note is lowercase l)-1: Re-reading the parameter settings file (similar to reload)---the corresponding signal is sighup-2: Represents the same action as the keyboard input CTRL + c---the corresponding signal is SIGINT-9: Immediately forcibly delete a job---the corresponding signal is Sigkill- the: Terminates a job in the normal way---the corresponding signal is sigterm [root@redflag~]# Jobs[1]+ Stopped vim BASHRC [root@redflag~]# kill-9%1 #以强制方式删除[1]+ has removed vim BASHRC [root@redflag~]# kill–sigterm%1 #-sigterm is the same as 15
Explain the difference between terminating a job in a normal and mandatory way: for example, vi when you edit a file .filename.swp , you generate a file, and if you end the job in the normal way, the file is deleted actively, and the .filename.swp task .filename.swp is ended in a forced way. The file will continue to exist in the file system.
Note: kill you can help us to send the signal to a job (%jobnumber) or some PID (direct input number), that is, the kill direct addition % of the number and the case is different.
Linux Job Management