背景
有時我們運(yùn)行某個任務(wù),需要保證任務(wù)不占據(jù)終端影響其他作業(yè),或者保證當(dāng)前session退出后,任務(wù)依然可以繼續(xù)運(yùn)行。
方案
- command &
- nohup command &
- CTRL+Z;bg %n; disown %n
- setsid command
使用方法
command &
該方法可以使任務(wù)放在后臺執(zhí)行,從而不影響當(dāng)前終端其他作業(yè),但是如果當(dāng)前終端退出,也會導(dǎo)致后臺任務(wù)接收到SIGHUP信號并退出。
-rwx------. 1 Appmanager devops 69 Jun 19 06:07 findBigFiles.bash
[appmanager@localhost toutiao]$ cat findBigFiles.bash
#! /bin/bash
sleep 1m
find $HOME -size +10M | xargs ls -l --block-size=M
[appmanager@localhost toutiao]$ ./findBigFiles.bash > output.log &
[1] 2644
## 退出當(dāng)前終端,新開終端并查看進(jìn)程
[appmanager@localhost ~]$ ps -fu appmanager
UID PID PPID C STIME TTY TIME CMD
appmana+ 2769 2768 0 06:08 pts/0 00:00:00 -bash
appmana+ 2818 2769 0 06:08 pts/0 00:00:00 ps -fu appmanager
## 重新后臺執(zhí)行程序
[appmanager@localhost toutiao]$ ps -fu appmanager
UID PID PPID C STIME TTY TIME CMD
appmana+ 2769 2768 0 06:08 pts/0 00:00:00 -bash
appmana+ 2889 2769 0 06:11 pts/0 00:00:00 /bin/bash ./findBigFiles.bash
appmana+ 2890 2889 0 06:11 pts/0 00:00:00 sleep 1m
appmana+ 2891 2769 0 06:12 pts/0 00:00:00 ps -fu appmanager
[appmanager@localhost toutiao]$ ps -fu appmanager
UID PID PPID C STIME TTY TIME CMD
appmana+ 2769 2768 0 06:08 pts/0 00:00:00 -bash
appmana+ 2906 2769 0 06:13 pts/0 00:00:00 ps -fu appmanager
[1]+ Done ./findBigFiles.bash > output.log
[appmanager@localhost toutiao]$ cat output.log
-rw-------. 1 appmanager devops 35M May 28 19:06 /home/appmanager/.cache/pip/http/d/6/1/f/e/d61fe4b09676f0692c240e67193e3af065d7b9e72f8577595151e51f
-rw-r--r--. 1 appmanager devops 11M May 16 08:49 /home/appmanager/mnt/ansible/roles/Tomcat/files/Apache-tomcat-8.5.79.tar.gz
nohup command &
nohup可以使shell命令忽略SIGHUP信號,避免父進(jìn)程退出時,子進(jìn)程也退出。&使任務(wù)后臺執(zhí)行。
[appmanager@localhost toutiao]$ nohup $HOME/tmp/toutiao/findBigFiles.bash > output.log 2>&1 &
## 退出當(dāng)前終端,新開終端并查看進(jìn)程
[appmanager@localhost ~]$ psfu
UID PID PPID C STIME TTY TIME CMD
appmana+ 3045 1 0 06:20 ? 00:00:00 /bin/bash /home/appmanager/tmp/toutiao/findBigFiles.bash
appmana+ 3046 3045 0 06:20 ? 00:00:00 sleep 1m
appmana+ 3102 3101 1 06:21 pts/0 00:00:00 -bash
appmana+ 3134 3102 0 06:21 pts/0 00:00:00 ps -fu appmanager
[appmanager@localhost ~]$ psfu
UID PID PPID C STIME TTY TIME CMD
appmana+ 3102 3101 0 06:21 pts/0 00:00:00 -bash
appmana+ 3154 3102 0 06:22 pts/0 00:00:00 ps -fu appmanager
[appmanager@localhost ~]$ cat tmp/toutiao/output.log
nohup: ignoring input
-rw-------. 1 appmanager devops 35M May 28 19:06 /home/appmanager/.cache/pip/http/d/6/1/f/e/d61fe4b09676f0692c240e67193e3af065d7b9e72f8577595151e51f
-rw-r--r--. 1 appmanager devops 11M May 16 08:49 /home/appmanager/mnt/ansible/roles/tomcat/files/apache-tomcat-8.5.79.tar.gz
CTRL+Z;bg %n; disown %n
使用disown可以忽略執(zhí)行jobs id接收到的SIGHUP信號,避免父進(jìn)程的退出而導(dǎo)致子進(jìn)程的退出。
## CTRL + 會暫停當(dāng)前執(zhí)行的程序
[appmanager@localhost toutiao]$ bash -x ./findBigFiles.bash > output.log 2>&1
^Z
[1]+ Stopped bash -x ./findBigFiles.bash > output.log 2>&1
[appmanager@localhost toutiao]$ bg %1
[1]+ bash -x ./findBigFiles.bash > output.log 2>&1 &
[appmanager@localhost toutiao]$ disown %1
## 退出當(dāng)前終端,新開終端并查看進(jìn)程
+ sleep 1m
+ xargs ls -l --block-size=M
+ find /home/appmanager -size +10M
-rw-------. 1 appmanager devops 35M May 28 19:06 /home/appmanager/.cache/pip/http/d/6/1/f/e/d61fe4b09676f0692c240e67193e3af065d7b9e72f8577595151e51f
-rw-r--r--. 1 appmanager devops 11M May 16 08:49 /home/appmanager/mnt/ansible/roles/tomcat/files/apache-tomcat-8.5.79.tar.gz
setsid command
setsid指定當(dāng)前進(jìn)程的父進(jìn)程為1,不受當(dāng)前session的退出的影響,但是前提是需要在命令執(zhí)行時就設(shè)置。
[appmanager@localhost toutiao]$ setsid ./findBigFiles.bash > output.log 2>&1 &
[1] 3823
[appmanager@localhost toutiao]$ psfu
UID PID PPID C STIME TTY TIME CMD
appmana+ 3729 3728 0 06:43 pts/0 00:00:00 -bash
appmana+ 3824 1 0 06:49 ? 00:00:00 /bin/bash ./findBigFiles.bash
appmana+ 3825 3824 0 06:49 ? 00:00:00 sleep 1m
appmana+ 3826 3729 0 06:49 pts/0 00:00:00 ps -fu appmanager
[1]+ Done setsid ./findBigFiles.bash > output.log 2>&1
## 退出當(dāng)前終端,新開終端并查看進(jìn)程
[appmanager@localhost toutiao]$ psfu
UID PID PPID C STIME TTY TIME CMD
appmana+ 3729 3728 0 06:43 pts/0 00:00:00 -bash
appmana+ 3824 1 0 06:49 ? 00:00:00 /bin/bash ./findBigFiles.bash
appmana+ 3825 3824 0 06:49 ? 00:00:00 sleep 1m
appmana+ 3827 3729 0 06:49 pts/0 00:00:00 ps -fu appmanager
[appmanager@localhost toutiao]$ ll
total 4
-rwx------. 1 appmanager devops 74 Jun 19 06:11 findBigFiles.bash
-rw-r--r--. 1 appmanager devops 0 Jun 19 06:49 output.log
[appmanager@localhost toutiao]$ cat /opt/
cat: /opt/: Is a directory
[appmanager@localhost toutiao]$ cat output.log
-rw-------. 1 appmanager devops 35M May 28 19:06 /home/appmanager/.cache/pip/http/d/6/1/f/e/d61fe4b09676f0692c240e67193e3af065d7b9e72f8577595151e51f
-rw-r--r--. 1 appmanager devops 11M May 16 08:49 /home/appmanager/mnt/ansible/roles/tomcat/files/apache-tomcat-8.5.79.tar.gz