1. Nohup
Nohup is undoubtedly the way we think first. As the name implies, Nohup's purpose is to let the submitted command ignore the hangup signal.
The use of nohup is very convenient, just add nohup before the command to be processed, standard output and standard error-deficient capital are redirected to the Nohup.out file. In general, we can add "&" at the end to run the command in the background as well as ">filename 2>&1" to change the default redirect file name.
nohup Example
[[email protected]~]# nohup ping www.ibm.com &
[1] 3059
nohup: appending output to `nohup.out‘
[[email protected]~]# ps -ef |grep 3059
root 3059 984 0 21:06 pts/3 00:00:00 ping www.ibm.com
root 3067 984 0 21:06 pts/3 00:00:00 grep 3059
[[email protected]~]#
The general form of the command is: Nohup Command &
If you submit a job using the Nohup command, all output from the job is redirected to a file named Nohup.out by default, unless the output file is specified separately:
Nohup command > Myout.file 2>&1 &
In the example above, the output is redirected to the Myout.file file.
Use jobs to view tasks.
Use FG%n to close.
When you successfully submit a process, a process number is displayed, which can be used to monitor the process, or to kill it. (Ps-ef | grep process number or kill-9 process number)
After using the nohup, a lot of people do not care, in fact, it is possible that the current account is not normal exit or end of the time, the command or the end of their own. So after running the command in the background with the Nohup command, you need to exit the current account normally with exit so that the command runs in the background.
CTRL + Z
You can place a command that is being executed in the foreground in the background and be in a paused state.
CTRL + C
Terminates the foreground command.
Jobs
See how many commands are currently running in the background.
The JOBS-L option shows the PID of all tasks, the status of jobs can be running, stopped, Terminated. However, if the task is terminated (kill), the shell removes the process identity of the task from the list known to the current shell environment.
2>&1 parsing
Command >out.file 2>&1 &
Command>out.file is to redirect the command output to the Out.file file, that is, the output is not printed to the screen, but is output to the Out.file file.
2>&1 is redirecting standard errors to standard output, where the standard output has been redirected to the Out.file file, and the standard error is output to the Out.file file. The last & is to have the command execute in the background.
Imagine what 2>1 stands for, 2 and > are represented by error redirection, while 1 represents an error redirect to a file 1 instead of standard output, and a combination of 2>&1,& and 1 represents the standard output, which becomes an error redirect to standard output.
Exit Task
If the task is running at the current shell terminal, the jobs command can be used to query the relevant information and kill the process.
# View the background run task process information for the current shell terminal
$ jobs
[1]+ Running nohup java-jar adapter-minisite.jar/tomcat-1/tomcat-2 > Logs.txt 2>&1 &
# Kill the task number #
$ kill%1
# Find PID
$ jobs-l
[1]+ 11076
$ kill 11076
#
$ FG%n # run for front end
Ctrl + C # exit
If not the current shell terminal, you can pass PS auxf | grep ' xxxxx ' gets PID and then kill PID.
2. Setsid
Nohup can undoubtedly make our process avoid interruption by ignoring the HUP signal, but if we think in a different way, if our process is not part of the sub-process of the terminal receiving the HUP signal, then naturally it will not be affected by the HUP signal. The use of Setsid is also very convenient, just add setsid before the command to be processed. Setsid Example
[[email protected]~]# setsid ping www.ibm.com
[[email protected]~]# ps -ef |grep www.ibm.com
root 31094 1 0 07:28 ? 00:00:00 ping www.ibm.com
root 31102 29217 0 07:29 pts/4 00:00:00 grep www.ibm.com
3. &
Here's a little tip about Subshell. We know that the inclusion of one or more names in the "()" allows these commands to run in a child shell, which expands a lot of interesting functionality, and one of the things we're going to talk about right now.
When we put "&" into "()", we will find that the submitted job is not in the job list, that is, it cannot be jobs viewed. Let's see why it's possible to avoid the effects of HUP signals.
Subshell Example
[[email protected]~]# (ping www.ibm.com &)
[[email protected]~]# ps -ef |grep www.ibm.com
root 16270 1 0 14:13 pts/4 00:00:00 ping www.ibm.com
root 16278 15362 0 14:13 pts/4 00:00:00 grep www.ibm.com4. Disown scene:
We already know that the effects of HUP signals can be avoided by adding nohup or setsid prior to the command. But if we have already submitted an order without any processing, how can we remedy it to avoid the effects of the HUP signal?
Solution: At this time to add nohup or setsid is too late, only through job scheduling and disown to solve the problem.
- Use
disown -h jobspec to make a job ignore the HUP signal.
- Use
disown -ah to make all jobs ignore the hup signal.
- Use
disown -rh to make the running job ignore the hup signal.
It is important to note that when disown is used, the target job will be removed from the job list and we will no longer be jobs able to use it to view it, but we can still ps -ef find it.
However, there is a problem, the operation of this method is the job, if we run the command at the end of the "&" to make it a job and run in the background, then it is all right, we can jobs get a list of all jobs by command. But if the current command is not running as a job, how can I get its job number? The answer is to use CTRL-Z (hold down the CTRL key while holding down the Z key)!
The purpose of Ctrl-z is to suspend the current process (Suspend), then we can use the jobs command to query its job number, then use bg jobspec it to put it in the background and continue to run. It is important to note that this method is used with caution if the suspension affects the running results of the current process.
5. Screen scene:
We already know how to keep the process free of HUP signals, but if there are a lot of such commands that need to be run in a stable background, how do you avoid doing this for every command?
Workaround:
The most convenient way to do this is screen. Simply put, screen provides the ansi/vt100 terminal emulator, which enables it to run multiple full screens of pseudo-terminals under a real terminal. Screen has a lot of parameters and is very powerful, so we'll just describe its common functions and briefly analyze why using screen can avoid the effects of HUP signals.
Using screen is convenient and has several common options:
- Use
screen -dmS session name to create a session in disconnected mode (and specify its session name).
- Use
screen -list to list all sessions.
- Use
screen -r session name to reconnect to the specified session.
- Use the shortcut key
CTRL-a d to temporarily disconnect the current session.
REF:
https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/
Nohup several ways to keep processes running reliably in the background