For faster navigation, this Iframe is preloading the Wikiwand page for Nohup.

Nohup

nohup 是一个 POSIX 命令,用于忽略 SIGHUP ("signal hang up" 译:挂断信号) 。 SIGHUP信号是终端注销时所发送至程序的一个信号。

nohup命令,在默认情况下(非重定向时),会输出一个名叫 nohup.out 的文件到终端上。

实例

[编辑]

下例中,nohup用于忽略SIGHUP信号,&使命令于后台执行,因此终端退出后命令仍旧执行。

$ nohup 命令名 &

值得注意,这种方法防止命令在注销时被忽略SIGHUP信号,但,如果该命令对 标准I/O文件(stdin,stdout,或stderr)进行输入/输出,那么该命令仍旧可能被终端挂起。[1] 详情请看下文的 阻止挂起 。 另外,nohup 命令常常和nice命令一起执行,以调整命令/程序的优先级。

$ nohup nice 命令 &

相关命令

[编辑]

一些shell(如 Bash)提供一个 shell builtin ,可用于防止发送 SIGHUP 而影响现有的jobs,即使没使用 nohup命令。

disown

[编辑]

在Bash,可以通过disown -h job名忽略 SIGHUP 信号;disown命令将移除job表中特定的job,这也意味着该job不再接受任何信号。[2][3]

通过 Control+Z 可以将当前进程挂起(放置后台并暂停运行),可通过 fg 命令恢复至前台,也通过bg将挂起的进程后台运行。

shopt

[编辑]

shopt huponexit命令让Bash在登录用的shell退出时,发送SIGHUP信号至所有的jobs。[4]

需注意的是,在使用 AIXSolaris 系统的nohup必须添加 p 选项。不同于上述bash内置命令disownnohup -p 使用的是进程ID。[5]

阻止挂断

[编辑]

注意,即便远程SSH会话注销或断开时,已经nohup且在后台job可以避免被中止。

另一个常见的问题是,SSH会话常常拒绝注销(或者挂起),因为它不愿意去丢失与后台job(s)进行交互的数据。[6][7] 该问题可通过 重定向 三次I/O流解决:待考证

$ nohup ./myprogram > foo.out 2> foo.err < /dev/null &

同时需要注意,关闭SSH会话不意味着会发送SIGHUP信号至对应程序。 除此以外,还取决于 伪终端 是否被分配。[8]

替代品

[编辑]
  • 一个 终端复用器(terminal multiplexer ) 可以执行命令于一个单独的会话,该会话是分离当前的终端的,这意味着如果当前终端结束后,分离的会话以及其关联程序仍保持运行。 之后,一个新的终端可以和该会话连接。[9]
例如,下例调用了 screen 将会运行"somescript.sh"于后台的一个独立的会话中:
$ screen -A -m -d -S somename ./somescript.sh &
  • disown 的命令是用于清除job表中的jobs,或者标记jobs,以便在会话结束时不发送SIGHUP信号至标记了的jobs。

参考文献

[编辑]
  1. ^ Re: nohup/disown and logout. Zsh.org. 2005-02-07 [2009-06-10]. (原始内容存档于2009-05-18). 
  2. ^ Linux / Unix: disown Command Examples. (原始内容存档于2020-05-18). 
  3. ^ bash(1): GNU Bourne-Again SHell - Linux man page. linux.die.net. [2020-01-10]. (原始内容存档于2019-12-27). 
  4. ^ 4.3.2 The Shopt Builtin. (原始内容存档于2019-12-26). 
  5. ^ IBM Knowledge Center页面存档备份,存于互联网档案馆). 01.ibm.com (2015-03-26). Retrieved on 2015-04-13.
  6. ^ SSH Frequently Asked Questions. Snailbook.com. [2009-06-10]. (原始内容存档于2009-01-22). 
  7. ^ OpenSSH FAQ. Openssh.com. 2005-09-20 [2009-06-10]. (原始内容存档于2009-07-10). 
  8. ^ Bug 396 – sshd orphans processes when no pty allocated. Bugzilla.mindrot.org. [2009-06-10]. (原始内容存档于2009-09-13). 
  9. ^ Linux 技巧:让进程在后台运行更可靠的几种方法. www.ibm.com. 2008-05-29 [2020-01-14]. (原始内容存档于2020-01-14) (中文(中国大陆)). 

外部链接

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
Nohup
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?