将此片段保存为名为snippet.sh
的文件:
nohup xmessage HI </dev/null &>/dev/null & disown
sleep 3
从同一目录执行xterm -e bash snippet.sh
。
[3秒钟后,xterm
窗口徘徊时xmessage
应该消失。一切都很好。但是,如果从片段文件中的命令中删除了nohup
,该怎么办?然后,xmessage
与xterm
一起消失。 bash
文档似乎表明,disown
本身应足以防止发送SIGHUP
:
The shell exits by default upon receipt of a SIGHUP. Before exiting, an interactive shell resends
the SIGHUP to all jobs, running or stopped. Stopped jobs are sent SIGCONT to ensure that they
receive the SIGHUP. To prevent the shell from sending the signal to a particular job, it should be
removed from the jobs table with the disown builtin (see SHELL BUILTIN COMMANDS below) or marked to
not receive SIGHUP using disown -h.
所以,问题是,如果没有xmessage
,为什么nohup
窗口不会停留?
disown
足以阻止shell发送信号,但不会阻止其他任何人。
特别是,shell是终端的控制过程,该终端的控制组中的进程为xmessage
,POSIX says:
如果该过程是控制过程,则将SIGHUP信号发送到属于呼叫过程的控制终端的前台过程组中的每个过程。