本文链接:http://codingstandards.iteye.com/blog/835536 (转载请注明出处)
nohup命令可以用来执行其他命令,并且忽略SIGHUP信号(run a command immune to hangups, with output to a non-tty。Run COMMAND, ignoring hangup signals.)当一个虚拟终端的网络连接断开时,操作系统向正在运行的程序发送SIGHUP信号(HUP = HangUP、挂断),默认情况下这个信号将使程序退出。
nohup命令通常用来把一个程序在后台来运行(需要&来配合),另外一个命令setsid也可以做到,如果系统中安装了screen,可以用screen来管理那些需要长时间运行的任务。本文后面提供的相关资料很详细的说明了这些命令,你也可以参考本系列之《我使用过的Linux命令之screen - 虚拟终端屏幕管理器 》。
常用格式如下:
nohup command [arg] ... &
运行命令command,忽略SIGHUP信号,后台执行。输出信息会保存到nohup.out文件中。`nohup' runs the given COMMAND with hangup signals ignored, so that the command can continue running in the background after you log out.
nohup COMMAND [ARG]...
以下内容来自 info nohup。
如果标准输入是终端,将被重定向为/dev/null。(If standard input is a terminal, it is redirected from `/dev/null' so that terminal sessions do not mistakenly consider the terminal to be used by the command. This is a GNU extension; programs intended to be portable to non-GNU hosts should use `nohup COMMAND [ARG]... </dev/null' instead.)
如果标准输出是终端,将被重定向到nohup.out,采用追加模式,如果这个文件不能写,就重定向到$HOME/nohup.out文件。(If standard output is a terminal, the command's standard output is appended to the file `nohup.out'; if that cannot be written to, it is appended to the file `$HOME/nohup.out'; and if that cannot be written to, the command is not run. Any `nohup.out' or `$HOME/nohup.out' file created by `nohup' is made readable and writable only to the user, regardless of the current umask settings.)
如果标准错误是终端,将被输出到标准输出同样的文件中。( If standard error is a terminal, it is redirected to the same file descriptor as the (possibly-redirected) standard output.)
nohup命令不会自动将命令后台运行,所以你必须自己在命令后面加上&,把命令变成后台执行。( `nohup' does not automatically put the command it runs in the background; you must do that explicitly, by ending the command line with an `&'. Also, `nohup' does not alter the niceness of COMMAND; use `nice' for that, e.g., `nohup nice COMMAND'.)
要注意的是,跟在nohup后面的命令不能是内建命令。(COMMAND must not be a special built-in utility (*note Special built-in utilities::).)
退出码:(Exit status: 126 if COMMAND is found but cannot be invoked,127 if `nohup' itself fails or if COMMAND cannot be found,the exit status of COMMAND otherwise)
[root@new55 test3]# nohup spider & tail -f nohup.out
[1] 18162
UUUUUUU [http://news.sina.com.cn/w/2010-09-20/084518138839s.shtml]
UUUUUUU [http://news.sina.com.cn/w/2010-06-20/090817680377s.shtml]
UUUUUUU [http://news.sina.com.cn/w/2010-06-18/164717673976s.shtml]
UUUUUUU [http://news.sina.com.cn/w/2010-06-18/075520496242.shtml]
UUUUUUU [http://news.sina.com.cn/w/2010-06-18/031720494028.shtml]
UUUUUUU [http://news.sina.com.cn/w/2010-06-18/101720497276.shtml]
UUUUUUU [http://i1.sinaimg.cn/dy/2010/0624/S12709T1277336424126.jpg]
UUUUUUU [hthttp://blog.sina.com.cn/s/blog_4a3131f00100j4d9.html?tj=1]
url=[hthttp://blog.sina.com.cn/s/blog_4a3131f00100j4d9.html?tj=1] schema[hthttp]
spider: test8.cpp:1051: int extract_url(const std::string&, PageInfo&, UrlPosMap&): Assertion `false' failed.
nohup: appending output to “nohup.out”
load spider.xml ok
<spider>
<start>
<url>http://www.sina.com.cn/</url>
</start>
<accept>
<prefix>http://www.sina.com.cn/</prefix>
<prefix>http://news.sina.com.cn/</prefix>
</accept>
<ignore>
<type>IMG</type>
</ignore>
</spider>
start node found
url http://www.sina.com.cn/
accept node found
prefix http://www.sina.com.cn/
prefix http://news.sina.com.cn/
ignore node found
type IMG
【1】einit 用nohup命令让Linux下程序永远在后台执行
【2】IBM Linux 技巧:让进程在后台可靠运行的几种方法
【3】IBM 学习 Linux,101: 创建、监控和终止进程
【4】IBM linux 技巧:使用 screen 管理你的远程会话