$ (.
/test
.sh &)
$ setsid .
/test
.sh &
$
nohup
.
/test
.sh &
Specific transfer from: http://digdeeply.org/archives/06281192.html
In Linux, if you want the process to run in the background, in general, we'll add & after the command, in fact, this is putting the command into a job queue:
Example
1 2 3) 4 5 |
$ . /test .sh & [1] 17208 $ jobs -l [1]+ 17208 Running . /test .sh & |
For commands that have already been executed in the foreground, they can also be re-executed in the background, first by pressing CTRL + Z to pause the already running process, and then using the BG command to put the stopped job in the background:
Example
1 2 3 4 5 6 7 8 |
$ . /test .sh [1]+ Stopped . /test .sh $ bg %1 [1]+ . /test .sh & $ jobs -l [1]+ 22794 Running . /test .sh & |
However, if the process is executed above the background, its parent process is still the process of the current terminal shell, and once the parent process exits, the hangup signal is sent to all child processes, and the child process will exit after receiving hangup. If we are going to continue running the process while exiting the shell, we need to use nohup to ignore the hangup signal, or SETSID to set the parent process to the INIT process (process number 1)
Example
1 2 3 4 5 6 7 8 9 |
$
echo $$ 21734
$
nohup .
/test
.sh & [1] 29016
$
ps -ef |
grep test 515 29710 21734 0 11:47 pts
/12
00:00:00
/bin/sh .
/test
.sh 515 29713 21734 0 11:47 pts
/12
00:00:00
grep test
|
Example
1 2 3 4 5 6 |
$ setsid. /test .sh & [1] 409 $ ps -ef | grep test 515 410 1 0 11:49 ? 00:00:00 /bin/sh /test .sh 515 413 21734 0 11:49 pts /12 00:00:00 grep test |
The above experiment demonstrates the use of Nohup/setsid plus & to make the process run in the background without being affected by the current shell exit. So what do you do with processes that are already running in the background? You can use the Disown command:
Example
1 2 3 4 5 6 7 8 9 10 11 |
$ .
/test
.sh & [1] 2539
$ jobs -l [1]+ 2539 Running .
/test
.sh &
$ disown -h %1
$
ps -ef |
grep test 515 410 1 0 11:49 ? 00:00:00
/bin/sh .
/test
.sh 515 2542 21734 0 11:52 pts
/12
00:00:00
grep test
|
There is also a way, even if the process is executed in a subshell, in fact, this is similar to Setsid. The method is simple, enclose the command in parentheses ():
Example
1 2 3 4 5 |
$ (. /test .sh &) $ ps -ef | grep test 515 410 1 0 11:49 ? 00:00:00 /bin/sh /test .sh 515 12483 21734 0 11:59 pts /12 00:00:00 grep test |
Note: The test environment for this article is red Hat Enterprise Linux as Release 4 (Nahant Update 5), Shell is/bin/bash, different OS and Shell may command somewhat differently. For example, Aix ksh, there is no disown, but you can use the Nohup-p PID to achieve disown the same effect.
There is also a more powerful way is to use screen, first create a disconnected mode of the virtual terminal, and then use the-r option to reconnect the virtual terminal, in which the execution of any command, can achieve the effect of nohup, which is more convenient when there are multiple commands in the background continuous execution:
Example
1 2 3 4 5 6 7 8 |
$ screen -dms screen_test $ screen -list there is a screen on:          27963.screen_test (Detached) 1 Socket in /tmp/uscreens/ S-jiangfeng $ screen -r screen_test |