How Linux runs and controls the background process: Nohup, Setsid, &, disown, screen
Transfer from http://heylinux.com/archives/1282.html#more-1282
We often encounter such problems, SSH login to the remote Linux server, run some time-consuming tasks, the result of the network and other instability caused by the task Midway failure.
This is because when the user logs off (logout) or the network disconnects, the terminal receives a HUP (hangup) signal to close all its child processes.
There are two possible workarounds: let the process ignore the HUP signal, or let the process run in a new session to become a child process that does not belong to this terminal.
Here is an introduction to the various methods of running and controlling the background process under Linux:
1.nohup
As the name implies, Nohup's purpose is to allow the submitted command to ignore all hangup signals.
How to use: Nohup COMMAND [ARG] ...
2.setsid
Run the command in a new session to avoid HUP signals from the current terminal.
How to use: Setsid COMMAND [ARG] ...
3.&
You can combine () to produce a new child shell and place the task in the background in this sub-shell, which is not affected by the hup signal of the current shell terminal.
How to use: (COMMAND [ARG] ... &)
And I usually use the following methods:
Nohup./filename.sh > Filename.log 2>&1 &
Three reasons:
1) nohup protection process will not be interrupted by hangup signal;
2) The task is placed in the background to run, do not occupy the current terminal;
3) Print error output to log, default > only standard output, error output not.
4. Control process
With the following command, we can control the commands placed in the background
To view background processes under the current terminal:
Direct execution: Jobs
Put a background process you see back into the foreground:
Direct input: FG {Jobid}//here {Jobid} is the number in the pre-process [] seen through the jobs command.
Put the process that is currently running in the foreground in the background to run:
First hit the shortcut key: Ctrl +z//pauses the currently running process.
Re-execution: BG
To terminate a process that is currently running in the foreground:
Just tap the shortcut: Ctrl +c
5.disown
The process of not using nohup and SETSID plus the ability to ignore the HUP signal.
How to use:
Put the process that is currently running in the foreground in the background (CTRL + Z and BG);
Then execute disown-h%{jobid}//here {Jobid} is the number in the before process [] seen through the jobs command.
6. Through screen to achieve a stable background operation
Screen is to create a new fullscreen virtual session terminal, this session will only exit when manually input exit, the command executed in this session do not worry about the HUP signal will affect our process, so do not have to add "nohup" or "setsid" before each command, Great for our planning to do a lot of background tasks, it is very convenient to let us manage these background tasks.
How to use:
Screen//creates and enters a session immediately.
SCREEN-DMS {Name}//Establish a session in disconnected mode and specify its session name according to our needs.
Screen-list//Lists all sessions.
screen-r {Name}//enters the specified session.
CTRL +ad//Enter shortcut keys CTRL +a and D to temporarily exit the current session.
Exit//Enter the specified session and execute exit to close the session.
Reference: https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/
Http://www.cnblogs.com/itech/archive/2012/09/16/2687404.html
How Linux runs and controls the background process: Nohup, Setsid, &, disown, screen