Linux job management, linux job
Before introducing job management, you must first know that job management isbashEnvironment. That is to say, "When you log on to the systembash shellThen, 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 currentbashSubroutineThat is, they are associated with each other. We cannottty1Environment to managetty2Ofbash.
Directly put the command in the background to "execute &
InbashIn the environment, the foreground refers to jobs that you can control. Backend: a job that can run on its own in the memory. You cannot directly control it unlessbg/fgAnd other commands to call the job.
Example:
[Root @ redflag ~] # Tar-zpcf/tmp/etc.tar.gz/etc & [1] 24874 <= [job number] PID [root @ redflag ~] # <= Can continue to work, not affected.
Herejob1Continue working in the background.linux. 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“[Ctrl]+c”This command is interrupted.
A problem occurs here, if the above command is
[root@redflag ~]# tar –zpcvf /tmp/etc.tar.gz /etc &
In this case, if the command is executed in the backgroundstdoutAndstderrThe data is output to the screen, so we cannot see the prompt, and we cannot control the foreground job in good condition. 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, all data is transmitted/tmp/log.txtAnd does not affect the front-end jobs.
Put the current job in the background and pause: [crtl] + Z
If you are in progressviBut to find a file, you needbashEnvironment search. You can leave nowvi
[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
In general, direct executionjobsYou can. In the preceding output (+ -. (+) Indicates the default job. Therefore, "Currently, I have two jobs in the background, both of which are paused. If I only enterfgThen[1]Will be processed at the front-end"
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 put in the background to "pause", whilebgTo run a job 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 caseStoppedChangedRunningAt the end of the command line&Indicates that the job is started in the background.
Note: operations from 1 to 5 are ongoing“%jobnumber”You can set“%”Omitted Direct AdditionjobnumberYou can.
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
The difference between terminating a job in a normal and forced way: for exampleviA.filename.swpFile. If the job ends normally.filename.swpThe file is automatically deleted. On the contrary, the job is ended in a forced way..filename.swpThe file continues to exist in the file system.
Note:killIt can help us send signals to a job.(%jobnumber)OrPID(Enter a number directly), that is,killAdd numbers and%The situations are different.