`

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

阅读更多

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

本文链接: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 管理你的远程会话

 

返回 我使用过的Linux命令系列总目录

 

0
0
分享到:
评论

相关推荐

    解决Python paramiko 模块远程执行ssh 命令 nohup 不生效的问题

    Python – paramiko 模块远程执行ssh 命令 nohup 不生效的问题解决 1、使用 paramiko 模块ssh 登陆到 linux 执行nohup命令不生效 # 执行命令 def command(ssh_config, cmd, result_print=None, nohup=False): ssh ...

    linux下nohup日志输出过大问题的解决方法

    linux下nohup日志输出过大问题的解决方法

    Linux-nohup日志拆分

    主要涉及Linux下对nohup的日志按照每天定时拆分生成新的日志并清空nohup原来的日志

    nohup命令详解.txt

    linux nohup 命令详解 自己留用

    详解linux下nohup日志输出过大问题解决方案

    主要介绍了详解linux下nohup日志输出过大问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    linux nohup及tail-f用法

    描述:nohup 命令运行由 Command 参数和任何相关的 Arg 参数指定的命令,忽略所有挂断(SIGHUP)信号。在注销后使用 nohup 命令运行后台中的程序。要运行后台中的 nohup 命令,添加 & ( 表示”and”的符号)到命令...

    Linux nohup实现后台运行程序及查看(nohup与&)

    一般运行linux上的程序都是执行 .sh 文件(./sh文件),那如果不影响当前CMD窗口的操作,需要后台运行怎么办呢? 这时就需要借助 nohup 和 & 命令来实现。 nohup java -server -Xms128M -Xmx512M -XX:MetaspaceSize...

    php守护进程 加linux命令nohup实现任务每秒执行一次

    Unix中 nohup 命令功能就是不挂断地运行命令,同时 nohup 把程序的所有输出到放到当前目录 nohup.out 文件中,如果文件不可写,则放到 &lt;用户主目录&gt;/nohup.out 文件中。那么有了这个命令以后我们php就写成shell ...

    linux后台执行命令&和nohup的具体使用方法

    当我们在终端或控制台工作时,可能不希望由于运行一个作业而占住了屏幕,因为可能还有...在后台运行作业时要当心:需要用户交互的命令不要放在后台执行,因为这样你的机器就会在那里傻等。不过,作业在后台运行一样会将

    通过nohup循环执行该脚本即可不间断执行(单纯的nohup不起作用时)

    当单纯的nohup命令不能起到在当前窗口关闭后仍可以执行某个操作或展示时,试试这个脚本吧,绝对可用。下载下来把里面的文件名称改一下,搜索的线程名字改一下,保存后用nohup执行,窗口关闭后也能保证程序不挂掉,好...

    Linux nohup命令原理及实例解析

    nohup命令可以将程序以忽略挂起信号的方式运行起来,被运行的程序的输出信息将不会显示到终端。 无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中。如果当前目录的 nohup.out ...

    一文明白使用nohup将服务器训练程序后台运行不关闭+随时通过查看训练情况输出与visdom可视化

    使用nohup将训练程序后台不间断运行+随时查看训练情况输出与可视化使用服务器进行远程训练nohup指令和查看输出缺省输出文件的写法指定输出文件的写法和CUDA一起用实时查看out配合visdom使用 使用服务器进行远程训练 ...

    nohup命令 后端运行程序

    nohup命令的全称为“no hang up”,该命令可以将程序以忽略挂起信号的方式运行起来,被运行的程序的输出信息将不会显示到终端。 无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件...

    常用linux命令小结

    我自己总结的linux常用命令,来源于日常积累的各种网页博客。因为不耐烦反复的查,索性自己总结了一个pdf,里面包括常用的linux命令,包括gcc,grep,tar,sed,find,screen,nohup,scp等,另外还有一些常用的文本处理...

    windows dos环境下直接运行linux命令集合

    使用方法,解压到本地目录,配置path,即可在dos下使用linux命令。 windows下能直接执行的linux命令,基本包括日常所有,如:[.exe grolbp.exe regtool.exe a2p.exe grolj4.exe rm.exe a2p5.10.0.exe grops.exe ...

    nohup使用方法LINUX|CENTOS

    入门

    nohup.out

    nohup.out

Global site tag (gtag.js) - Google Analytics