Linux nohup 和 & 和有什么不一样

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15595374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:32:40  来源:igfitidea点击:

What's the difference between nohup and ampersand

linuxbashnohup

提问by Yarkee

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 myprocess.out &myprocess.out &将 myprocess.out 设置为在后台运行。关闭终端后,该进程仍在运行。它们之间有什么区别?

采纳答案by nemo

nohupcatches the hangup signal (see man 7 signal) while the ampersand doesn't (except the shell is confgured that way or doesn't send SIGHUPat all).

nohup捕获挂断信号(请参阅 参考资料man 7 signal),而 & 符号不捕获(除了外壳以这种方式配置或根本不发送SIGHUP)。

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.

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

In case you're using bash, you can use the command shopt | grep huponto 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.

如果您正在使用 bash,您可以使用该命令shopt | grep hupon来确定您的 shell 是否向其子进程发送 SIGHUP。如果它关闭,进程将不会被终止,就像你的情况一样。可以在此处找到有关 bash 如何终止应用程序的更多信息。

There are cases where nohupdoes not work, for example when the process you start reconnects the SIGHUPsignal, as it is the case here.

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

回答by amit_g

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 nohupcommand ignores the HUPsignal and thus even if the current shell is terminated, the subshell and myprocess.outwould 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.outor $HOME/nohup.out.

myprocess.out &将使用子外壳在后台运行该进程。如果当前 shell 被终止(比如注销),所有子 shell 也会被终止,因此后台进程也将被终止。的nohup的命令忽略HUP信号,因此,即使当前壳被终止时,子外壳和myprocess.out将继续在后台运行。另一个区别是&单独不会重定向 stdout/stderr 所以如果有任何输出或错误,它们会显示在终端上。另一方面, nohup 将 stdout/stderr 重定向到nohup.outor $HOME/nohup.out

回答by Michel

Using the ampersand (&) will run the command in a child process (child to the current bash session). However, when you exit the session, all child processes will be killed.

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

using nohup + ampersand (&) will do the same thing, except that when the session ends, the parent of the child process will be changed to "1" which is the "init" process, thus preserving the child from being killed.

使用 nohup + &符号(&)会做同样的事情,除了当会话结束时,子进程的父进程将更改为“1”,即“init”进程,从而防止子进程被杀死。

回答by missgeorge

Most of the time we login to remote server using ssh. If you start a shell script and you logout then the process is killed. Nohup helps to continue running the script in background even after you log out from shell.

大多数时候我们使用 ssh 登录到远程服务器。如果您启动 shell 脚本并注销,则该进程将被终止。即使从 shell 注销后,Nohup 也有助于在后台继续运行脚本。

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

Nohup catches the HUP signals. Nohup doesn't put the job automatically in the background. We need to tell that explicitly using &

Nohup 捕捉 HUP 信号。Nohup 不会自动将作业置于后台。我们需要明确地使用 &

回答by santosh

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 命令是一个信号屏蔽实用程序,可以捕获挂断信号。因为&符号没有捕捉到挂断信号。当使用 & 运行命令并退出 shell 时,shell 将使用挂断信号终止子命令。这可以通过使用 nohup 来防止,因为它会捕获信号。Nohup 命令接受可以由内核发送到进程并阻止它们的挂断信号。当用户想要启动长时间运行的应用程序注销或关闭启动进程的窗口时,Nohup 命令很有用。这些操作中的任何一个通常都会提示内核挂断应用程序,但 nohup 包装器将允许进程继续。使用&符号将在子进程和当前 bash 会话的这个子进程中运行命令。当您退出会话时,该进程的所有子进程都将被杀死。&符号与活动外壳的作业控制有关。这对于在后台运行会话中的进程很有用。

回答by John Joe

Correct me if I'm wrong

如果我错了纠正我

  nohup myprocess.out &

nohupcatches the hangup signal, which mean it will send a process when terminal closed.

nohup捕获挂断信号,这意味着它会在终端关闭时发送一个进程。

 myprocess.out &

Process can run but will stopped once the terminal is closed.

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

nohup myprocess.out

Process able to run even terminal closed, but you are able to stop the process by pressing ctrl+ zin terminal. Crt+znot working if &is existing.

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

回答by Devender Goyal

There are many cases when small differences between environments can bite you. This is one into which I have ran recently. What is the difference between these two commands?

在很多情况下,环境之间的细微差异都会让您感到厌烦。这是我最近跑到的一个。这两个命令有什么区别?

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

The answer is the same as usual - it depends.

答案和往常一样——这取决于。

nohup catches the hangup signal while the ampersand does not.

nohup 捕获挂断信号,而&符号则没有。

What is the hangup signal?

什么是挂断信号?

SIGHUP - hangup detected on controlling terminal or death of controlling process (value: 1).

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

Normally, when running a command using & and exiting the shell afterwards, the shell will terminate the sub-command with the hangup signal (like 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.

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

Fine, but like in this case there are always ‘buts'. There is no difference between these launching methods when the shell is configured in a way where it does not send SIGHUP at all.

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

In case you are using bash, you can use the command specified below to find out whether your shell sends SIGHUP to its child processes or not:

如果您正在使用 bash,您可以使用下面指定的命令来确定您的 shell 是否向其子进程发送 SIGHUP:

~ $ shopt | grep hupon

And moreover - there are cases where nohup does not work. For example, when the process you start reconnects the NOHUP signal (it is done inside, on the application code level).

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

In the described case, lack of differences bit me when inside a custom service launching script there was a call to a second script which sets up and launches the proper application without a nohup command.

在所描述的情况下,当在自定义服务启动脚本中调用第二个脚本时,缺乏差异让我感到困惑,该脚本在没有 nohup 命令的情况下设置和启动正确的应用程序。

On one Linux environment everything worked smoothly, on a second one the application quit as soon as the second script exited (detecting that case, of course took me much more time then you might think :stuck_out_tongue:).

在一个 Linux 环境中,一切运行顺利,在第二个环境中,应用程序在第二个脚本退出时立即退出(检测这种情况,当然比你想象的要花费更多的时间:stuck_out_tongue:)。

After adding nohup as a launching method to second script, application keeps running even if the scripts will exit and this behavior became consistent on both environments.

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