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


当前回答

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不工作。

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.

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

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

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

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

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

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

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

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

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