有时我们运行某个任务,需要保证任务不占据终端影响其他作业,或者保证当前session退出后,任务依然可以继续运行。
command &
该方法可以使任务放在后台执行,从而不影响当前终端其他作业,但是如果当前终端退出,也会导致后台任务接收到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
## 退出当前终端,新开终端并查看进程
[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
## 重新后台执行程序
[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可以使shell命令忽略SIGHUP信号,避免父进程退出时,子进程也退出。&使任务后台执行。
[appmanager@localhost toutiao]$ nohup $HOME/tmp/toutiao/findBigFiles.bash > output.log 2>&1 &
## 退出当前终端,新开终端并查看进程
[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
使用disown可以忽略执行jobs id接收到的SIGHUP信号,避免父进程的退出而导致子进程的退出。
## CTRL + 会暂停当前执行的程序
[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
## 退出当前终端,新开终端并查看进程
+ 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指定当前进程的父进程为1,不受当前session的退出的影响,但是前提是需要在命令执行时就设置。
[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
## 退出当前终端,新开终端并查看进程
[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