Sometimes it takes a long time to run a program, so it's a bit crazy to get off the clock or when the network is unstable. Today I'm going to share a few of the methods I've used in my work to keep the program running in the background.
Nohup
$ nohup--husage:nohup COMMAND [ARG] ... Or: nohup optionrun COMMAND, ignoring hangup signals. --help Display this help and exit --version output version information and exit
When running a program, we exit the current account, or close the terminal, the program will receive a SIGHUP signal, nohup is to ignore this signal, so that the program does not hang. Nohup is very simple to use, namely: Nohup command. There are several tips for using the process:
1. Simultaneous use of Nohup and &
How to use: Nohup Command &
When exiting the current account or closing the terminal, the program will receive the SIGHUP signal, this signal will be used by the NOHUP program ignored, but if you use the CTRL + C end command, the program will receive the SIGINT signal, the signal will still let the program end, if you do not want the program to be ended, Add a & at the end, and let the program also ignore the SIGINT signal.
2. REDIRECT standard output and standard error
How to use: Nohup command > FileName 2<&1 &
Here is the file descriptor, 0 for stdin standard input, 1 for stdout standard output, 2 for stderr standard error, and one/dev/null for empty device files.
Nohup output to the Nohup.out file by default, if you want to redirect output to another file, you need to add > filename after the nohup command, 2<&1, to redirect the standard error to the standard output.
Setsid
$ setsid--husage:setsid [options] <program> [arguments ...] Run a program in a new session. Options:-C,--ctty set the controlling terminal to the current one-w,--wait wait program to exit, and use the S Ame Return-h,--help Display this help and exit-v,--version output version information and exit
Nohup prevents the process from being interrupted halfway by ignoring the HUP signal, and is not affected by the HUP signal if our process is not part of the terminal subprocess that accepts the HUP signal. Using Setsid can help us do this.
How to use: Setsid command
Several ways to run Linux---programs in the background