---
title: "我使用过的Linux命令之nohup - 执行其他命令，并且忽略SIGHUP信号"
author: "codingstandards"
date: "2010-12-07T13:28:42Z"
tags: ["Blog", "F#", "IBM", "Linux"]
categories: ["操作系统"]
slug: "linux-nohup-sighup"
draft: false
lastmod: "2010-12-07T13:29:02Z"
id: 835536
url: "https://codingstandards.iteye.com/blog/835536"
---

# 我使用过的Linux命令之nohup - 执行其他命令，并且忽略SIGHUP信号

本文链接：[http://codingstandards.iteye.com/blog/835536](/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 - 虚拟终端屏幕管理器](/blog/781839) 》。

 

## 常用参数

常用格式如下：

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]... &lt;/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  
&lt;spider&gt;  
    &lt;start&gt;  
        &lt;url&gt;http://www.sina.com.cn/&lt;/url&gt;  
    &lt;/start&gt;  
    &lt;accept&gt;  
        &lt;prefix&gt;http://www.sina.com.cn/&lt;/prefix&gt;  
        &lt;prefix&gt;http://news.sina.com.cn/&lt;/prefix&gt;  
    &lt;/accept&gt;  
    &lt;ignore&gt;  
        &lt;type&gt;IMG&lt;/type&gt;  
    &lt;/ignore&gt;  
&lt;/spider&gt;  
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下程序永远在后台执行](http://www.einit.com/user1/11/archives/2006/3603.html)      
【2】IBM  [Linux 技巧：让进程在后台可靠运行的几种方法](http://www.ibm.com/developerworks/cn/linux/l-cn-nohup/)      
【3】IBM  [学习 Linux，101: 创建、监控和终止进程](http://www.ibm.com/developerworks/cn/linux/l-lpic1-v3-103-5/)       
【4】IBM  [linux 技巧：使用 screen 管理你的远程会话](http://www.ibm.com/developerworks/cn/linux/l-cn-screen/)

 

返回 [我使用过的Linux命令系列总目录](/blog/786653)