背景

  我们通常使用&将前台任务变为后台任务执行,但是如果只是使用&,那么在突然断网或者关闭启动该任务的终端(ps:可使用putty来测试,部分软件如mobaxterm做了优化,关闭终端是友好关闭的)时,内核就会给后台任务发送SIGHUP信号,从而导致后台任务停止。这时,我们就需要使用nohup来启动该后台任务。

简介

  nohup,顾名思义,就是使得运行的命令可以忽略HANGUP信号。因此,即使突然断网或者关闭终端,该后台任务依然可以继续执行。

  这里需要指明的是,nohup并不会自动将任务运行在后台,我们需要在命令行末尾加上&来显示的指明。

进阶

  如果nohup命令的标准输入是终端,那么nohup将会默认使用/dev/null来重定向。   如果nohup命令的标准输出是终端,那么标准输出会被附加到文件nohup.out中;如果用户没有在当前目录创建文件的权限,那么就会把输出附加到$HOME/nohup.out中;如果还是没有写入权限,那么该命令就不会执行。

  如果nohup命令的标准错误是终端,那么就会被定向到标准输出的附加的文件描述符。如果标准输出被关闭了,那么标准错误就会像上面一样尝试附加到nohup.out$HOME/nohup.out中。

测试

  这里,我们先创建一个需要较长时间来执行的脚本。该脚本会打印一个数字(标准输出),删除一个不存在的文件(标准错误输出)。

$ vi test.sh
for i in `seq 1 20`; do echo $i; rm a.txt; sleep 1; done

默认参数

  我们来后台执行该文件。

$ nohup sh test.sh &
[1] 9119
$ nohup: ignoring input and appending output to 'nohup.out'

  在上面执行的命令中,由输出nohup: ignoring input and appending output to 'nohup.out'可知,标准输出被重定向到nohup.out中,我们查看下该文件。

$ head nohup.out
1
rm: cannot remove 'a.txt': No such file or directory
2
rm: cannot remove 'a.txt': No such file or directory
3
rm: cannot remove 'a.txt': No such file or directory
4
rm: cannot remove 'a.txt': No such file or directory
5
rm: cannot remove 'a.txt': No such file or directory
...

  该文件包含了命令的标准输出和标准错误输出。

重定向标准输出

$ nohup sh test.sh 1>o.out &
[1] 9378
$ nohup: ignoring input and redirecting stderr to stdout

  在上面执行的命令中,可知,标准输出被重定向到stdout中,而标准输出又重定向到了o.out中,我们查看下该文件。

$ head o.out
1
rm: cannot remove 'a.txt': No such file or directory
2
rm: cannot remove 'a.txt': No such file or directory
3
rm: cannot remove 'a.txt': No such file or directory
4
rm: cannot remove 'a.txt': No such file or directory
5
rm: cannot remove 'a.txt': No such file or directory
...

  该文件包含了标准输出和标准错误输出的。

重定向标准输出标准错误输出

$ nohup sh test.sh 1>o.out 2>e.out &
[1] 9490

  这一次,终端没有其他输出。我们直接查看命令行中的两个文件。

$ head o.out
1
2
3
4
5
6
7
8
9
10 $ head e.out
nohup: ignoring input
rm: cannot remove 'a.txt': No such file or directory
rm: cannot remove 'a.txt': No such file or directory
rm: cannot remove 'a.txt': No such file or directory
rm: cannot remove 'a.txt': No such file or directory
rm: cannot remove 'a.txt': No such file or directory
rm: cannot remove 'a.txt': No such file or directory
rm: cannot remove 'a.txt': No such file or directory
rm: cannot remove 'a.txt': No such file or directory
rm: cannot remove 'a.txt': No such file or directory

shell脚本中执行nohup的问题

  如果我们在shell中执行nohup命令,并且没有进行任何重定向,那么终端上就会弹出“nohup: ignoring input and appending output to 'nohup.out'”,并且,只有敲击回车,shell才能继续执行,否则就会卡住。

  经过试验,发现只要重定向了标准错误输出,终端就不会弹出任何提示,也不会卡住。但是标准错误附加的文件会出现其他的错误提示,只有重定向所有标准输入、标准输出和标准错误输出后,不会有任何错误提示。如:

$ nohup sh test.sh >out.log 2>&1 </dev/null &

参考

http://www.cnblogs.com/allenblogs/archive/2011/05/19/2051136.html

http://www.cnblogs.com/lovemo1314/archive/2011/07/13/2105472.html

http://www.gnu.org/software/coreutils/manual/html_node/nohup-invocation.html#nohup-invocation

https://stackoverflow.com/questions/24646320/nohupignoring-input-and-appending-output-to-nohup-out

nohup介绍的更多相关文章

  1. Ubuntu安装设置nginx和nohup常用操作

    nginx安装 Ubuntu直接从常规源中安装 apt-get install nginx 安装的目录 配置文件:/etc/nginx/ 主程序文件:/usr/sbin/nginx Web默认目录:/ ...

  2. Kafka远程调试简单记录

    Kafka启动脚本: ./kafka-server-start.sh -daemon ../config/server.properties 最终翻阅脚本可以确定是调用kafka-run-class. ...

  3. 实用技巧:简单而有用的nohup命令介绍(转)

    简单而有用的nohup命令在UNIX/LINUX中,普通进程用&符号放到后台运行,如果启动该程序的控制台logout,则该进程随即终止. 要实现守护进程,一种方法是按守护进程的规则去编程(本站 ...

  4. Linux的bg和fg和jobs和nohup命令简单介绍

    我们都知道,在 Windows 上面,我们要么让一个程序作为服务在后台一直运行,要么停止这个服务.而不能让程序在前台后台之间切换.而 Linux 提供了 fg 和 bg 命令,让我们轻松调度正在运行的 ...

  5. Linux监控工具介绍系列——OSWatcher Black Box

      OSWatcher Balck Box简介 OSWatcher Black Box (oswbb)是Oracle开发.提供的一个小巧,但是实用.强大的系统工具,它可以用来抓取操作系统的性能指标,用 ...

  6. Linux:使用nohup让进程在后台可靠运行

    学习之余我最大的乐趣是找一部不错的电影慢慢品味,这也是我缓解压力的最好方式之一,由于我常去的字幕组网站需要签到才可以下载字幕,像这种娱乐网站谁有时间天天记得去签到呢,but作为一个准程序猿应该有更好的 ...

  7. Linux运行与控制后台进程的方法:nohup, setsid, &, disown, screen

    我们经常会碰到这样的问题,用ssh登录了远程的Linux服务器,运行了一些耗时较长的任务,结果却由于网络等的不稳定导致任务中途失败.这是由于在用户注销(logout)或者网络断开时,终端会收到 HUP ...

  8. UiAutomator自动化测试框架介绍

    UiAutomator自动化测试框架介绍 环境搭建 1         必要条件 1.1       JDK 1.2       SDK(API高于15) 1.3       Eclipse 2    ...

  9. linux 后台运行命令 nohup命令

    转载:http://if.ustc.edu.cn/~ygwu/blog/archives/000538.html 2005年04月18日 简单而有用的nohup命令在UNIX/LINUX中,普通进程用 ...

随机推荐

  1. javascript对象的创建--相对java 怎样去创建了"类"i以及实例化对象

    由于javascript没有java那么多基本类型,同时也没有提供class这个东西,那么我们想实现javascript的对象创建应该怎么办呢,我简单地从w3c提供的课件中提取了一下几种方法: 一.工 ...

  2. Spring核心--IOC

    先说说Spring框架 Spring框架 Spring框架的作用 Spring 的核心(IOC功能) SpringAOP功能 原有的业务流程:请求->servlet-->service-- ...

  3. 再起航,我的学习笔记之JavaScript设计模式03

    我的学习笔记是根据我的学习情况来定期更新的,预计2-3天更新一章,主要是给大家分享一下,我所学到的知识,如果有什么错误请在评论中指点出来,我一定虚心接受,那么废话不多说开始我们今天的学习分享吧! 上一 ...

  4. js判断空值

    { "mDataProp": 'CreationTime', 'mRender': function (date) { if (!date && typeof (d ...

  5. jQuery基础知识总结二

     * DOM操作 *1 基本操作 * html() - 等价于innerHTML属性 * text() - 等价于textContent属性 * val() - 等价于value属性 * attr() ...

  6. OpenOfice将offic转为pdf并且在web显示

    1.将office首先要安装OpenOfice,傻瓜式安装就好了,之后可以使用下列代码将word转为pdf.这个需要导入jodconverter-2.2.2里的 ja r包 import java.i ...

  7. NYOJ 5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  8. 解决js中post提交数据并且跳转到指定页面的问题总结

    今天在开发中过程中遇到了这个问题,js中利用JQuery中的 $.post("url", id, function(){}); 这个方法是数据提交正常,但是后台处理完成之后跳转无法 ...

  9. Linux下ftp的安装配置

    1.查看ftp包是否可用yum list | grep vsftpd 2.安装ftpyum install vsftpd 3.启动systemctl start vsftpd 4.开机启动chkcon ...

  10. Linux改变语言设置的命令

    --Linux语言设置--------------2013/09/22Linux中语言的设置和本地化设置真是一个很繁琐的事情,时不时的会出现乱码的情况,在这篇文章中讨论的是shell中出现乱码的一些解 ...