nohup 和 & 和有什么不一样 [英] What's the difference between nohup and ampersand
问题描述
nohup myprocess.out &
或 myprocess.out &
都将 myprocess.out 设置为在后台运行.关闭终端后,该进程仍在运行.它们有什么区别?
Both nohup myprocess.out &
or myprocess.out &
set myprocess.out to run in the background. After I shutdown the terminal, the process is still running.
What's the difference between them?
推荐答案
nohup
捕获挂断信号(参见 man 7 signal
),而 & 符号没有(除了外壳是这样配置的,或者根本不发送 SIGHUP
).
nohup
catches the hangup signal (see man 7 signal
) while the ampersand doesn't (except the shell is confgured that way or doesn't send SIGHUP
at all).
通常,当使用 &
运行命令并随后退出 shell 时,shell 将使用挂断信号终止子命令(kill -SIGHUP
nohup
来防止,因为它会捕获信号并忽略它,因此它永远不会到达实际应用程序.
Normally, when running a command using &
and exiting the shell afterwards, the shell will terminate the sub-command with the hangup signal (kill -SIGHUP <pid>
). This can be prevented using nohup
, as it catches the signal and ignores it so that it never reaches the actual application.
如果您使用的是 bash,您可以使用命令 shopt |grep hupon
查看是否您的 shell 是否向其子进程发送 SIGHUP.如果它关闭,进程将不会已终止,因为您的情况似乎如此.有关 bash 如何终止的更多信息可以在此处找到应用程序.
In case you're using bash, you can use the command shopt | grep hupon
to find out whether
your shell sends SIGHUP to its child processes or not. If it is off, processes won't be
terminated, as it seems to be the case for you. More information on how bash terminates
applications can be found here.
在某些情况下 nohup
不起作用,例如当您启动的进程重新连接时SIGHUP
信号,因为它是这种情况 此处.
There are cases where nohup
does not work, for example when the process you start reconnects
the SIGHUP
signal, as it is the case here.
这篇关于nohup 和 & 和有什么不一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!