两者都不符合我的流程。Out & or myprocess。出&设置我的进程。在后台运行。关闭终端后,进程仍在运行。 它们之间有什么区别?


nohup捕获挂起信号(参见man 7信号),而&符号不捕获(除非shell以这种方式配置或根本不发送SIGHUP)。

正常情况下,当使用&运行命令并退出shell时,shell将用挂起信号(kill -SIGHUP <pid>)终止子命令。可以使用nohup来防止这种情况,因为它捕获信号并忽略它,因此它永远不会到达实际应用程序。

如果您正在使用bash,您可以使用命令shop | grep hupon来确定是否 你的shell是否发送SIGHUP给它的子进程。如果它是关闭的,进程将不会 终止了,就像你的情况一样。关于bash如何终止的更多信息 应用程序可以在这里找到。

在某些情况下,nohup不起作用,例如当您启动的进程重新连接时 SIGHUP信号,就像这里的情况一样。

myprocess.out & would run the process in background using a subshell. If the current shell is terminated (say by logout), all subshells are also terminated so the background process would also be terminated. The nohup command ignores the HUP signal and thus even if the current shell is terminated, the subshell and myprocess.out would continue to run in the background. Another difference is that & alone doesn't redirect the stdout/stderr so if there are any output or error, those are displayed on the terminal. nohup on the other hand redirect the stdout/stderr to nohup.out or $HOME/nohup.out.

使用&号(&)将在子进程(当前bash会话的子进程)中运行该命令。但是,当您退出会话时,所有子进程将被杀死。

使用nohup + &(&)将完成同样的事情,除了会话结束时,子进程的父进程将被更改为“1”,即“init”进程,从而保护子进程不被杀死。

大多数情况下,我们使用ssh登录远程服务器。如果你启动一个shell脚本,然后注销,那么这个进程就会被杀死。 Nohup帮助在退出shell后继续在后台运行脚本。

Nohup command name &
eg: nohup sh script.sh &

Nohup接收HUP信号。 Nohup不会自动把工作放在后台。我们需要使用&显式地告诉它

The nohup command is a signal masking utility and catches the hangup signal. Where as ampersand doesn’t catch the hang up signals. The shell will terminate the sub command with the hang up signal when running a command using & and exiting the shell. This can be prevented by using nohup, as it catches the signal. Nohup command accept hang up signal which can be sent to a process by the kernel and block them. Nohup command is helpful in when a user wants to start long running application log out or close the window in which the process was initiated. Either of these actions normally prompts the kernel to hang up on the application, but a nohup wrapper will allow the process to continue. Using the ampersand will run the command in a child process and this child of the current bash session. When you exit the session, all of the child processes of that process will be killed. The ampersand relates to job control for the active shell. This is useful for running a process in a session in the background.

如果我说错了,请指正

  nohup myprocess.out &

Nohup捕获挂起信号,这意味着当终端关闭时它将发送一个进程。

 myprocess.out &

进程可以运行,但是一旦终端关闭,进程就会停止。

nohup myprocess.out

即使终端关闭,进程也能运行,但你可以在终端中按ctrl + z来停止进程。如果&存在,Crt +z不工作。

在很多情况下,环境之间的微小差异会让你大吃一惊。这是我最近遇到的一个问题。这两个命令有什么区别?

1 ~ $ nohup myprocess.out &
2 ~ $ myprocess.out &

答案和往常一样——视情况而定。

Nohup捕获挂机信号,而&号则没有。

挂机信号是什么?

SIGHUP -在控制终端上检测到挂起或控制进程死亡(值:1)。

通常,当使用&运行命令并退出shell时,shell将使用挂起信号终止子命令(如kill -SIGHUP $PID)。可以使用nohup来防止这种情况,因为它捕获信号并忽略它,因此它永远不会到达实际应用程序。

好吧,但就像在这种情况下总是有“但是”。当shell配置为完全不发送SIGHUP时,这些启动方法之间没有区别。

如果你正在使用bash,你可以使用下面指定的命令来查看你的shell是否将SIGHUP发送给它的子进程:

~ $ shopt | grep hupon

此外,在某些情况下nohup不起作用。例如,当您启动的进程重新连接NOHUP信号时(这是在应用程序代码级别内部完成的)。

在上述案例中,在自定义服务启动脚本中,由于缺少差异,在没有nohup命令的情况下调用了第二个脚本来设置和启动适当的应用程序。

在一个Linux环境中,一切都很顺利,而在另一个环境中,当第二个脚本退出时,应用程序就退出了(检测这种情况,当然花费了我更多的时间,你可能会认为:stuck_out_tongue:)。

在将nohup作为启动方法添加到第二个脚本之后,即使脚本将退出,应用程序也会继续运行,并且这种行为在两个环境中都是一致的。