1 overview
Linux jobs have two ways, one is working in the foreground, the other is working in the background
Front-end operation: Through the terminal start, and after the start has occupied the terminal;
Background jobs: Can be started through the terminal, but after the boot into the background to run (release terminal)
2 Let the job run in the background
(1) Operation: Ctrl + Z, the program is placed in the background, but does not execute, the status is stop
(2) Jobs that have not been started: COMMAND & Run the program directly in the background
The above two ways background work is sent to the background, but it is still related to the terminal; exiting the terminal will close the background job.
If you want to split the relationship with the terminal after sending it to the background, run the statement in the following two ways
Method One
Nohup COMMAND &>/dev/null &
Method Two
Execute screen First
Screen
Then execute the command under the Screen window. Do not perform the abort command in this screen window
COMMAND;
Close the screen window or exit the terminal directly, the command will still be executed in the background.
It is recommended to use screen to run the command, do not need to see the output of the command, and also need to operate in the window
It is recommended to enter command &>/dev/null & this command to run directly in the background without interfering with the current screen operation
This way you can also enter multiple background run commands in the screen at the same time
With screen execution command, will make the command in the background execution, even if the network, the background can still execute commands, when the terminal is disconnected, screen process will be independent, so the command will continue to run
Attention
Command & executed on the background of 6, the terminal network is disconnected, the command will continue to execute
Command & executed on the background of 7, the terminal network is disconnected, the command does not continue, it is recommended to use screen or call Nohup the way to execute
3 Job Control
3.1 Viewing jobs
View all jobs in the current terminal, execute command jobs
If you have closed the terminal, but to view the PID of the command, you can use the following command process to view
Ps-ef | grep CMD
3.2 Job Control
3.2.1 FG
Fg[[%]job_num]: Bring the assigned background job back to the foreground
First use the job to see if there is a process in the background, assuming that there is a process ID 2 in the background, whether it is running or stop just use
FG 2, will be transferred to the foreground to run, but, after the front office, press CTRL + C will stop the process
3.2.2 BG
Bg[[%]job_num]: Let the job sent to the background continue to run in the background
Assuming that the terminal front desk has a command running and is pressed CTRL + Z at this point, the command will be placed in the background, but the state is stopped
With the command BG 1 will again put the instructions in the background to run, at this time ctrl-c can not stop the command, if it is a normal window Run command,
Then shutting down the terminal will cause the background command to stop running.
However, if you are running BG under screen, you must stop the command by killing the ID of the corresponding process
3.2.3 Kill
Kill [%job_num]: terminates the specified job
Kill Signal PID
If kill-9 1028 kills process number 1028
kill-19 cmd Put the command in the background and hibernate .
kill-18 cmd Wake the command to run in the background
This article is from the "Self-learning Linux" blog, so be sure to keep this source http://ghbsunny.blog.51cto.com/7759574/1959590
Job Control for Linux