nohup介绍

来源:互联网 发布:手机视频录播软件 编辑:程序博客网 时间:2024/06/15 20:19

背景

  我们通常使用&将前台任务变为后台任务执行,但是如果只是使用&,那么在突然断网或者关闭启动该任务的终端(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.shfor 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.out1rm: cannot remove 'a.txt': No such file or directory2rm: cannot remove 'a.txt': No such file or directory3rm: cannot remove 'a.txt': No such file or directory4rm: cannot remove 'a.txt': No such file or directory5rm: 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.out1rm: cannot remove 'a.txt': No such file or directory2rm: cannot remove 'a.txt': No such file or directory3rm: cannot remove 'a.txt': No such file or directory4rm: cannot remove 'a.txt': No such file or directory5rm: cannot remove 'a.txt': No such file or directory...

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

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

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

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

$ head o.out12345678910$ head e.outnohup: ignoring inputrm: cannot remove 'a.txt': No such file or directoryrm: cannot remove 'a.txt': No such file or directoryrm: cannot remove 'a.txt': No such file or directoryrm: cannot remove 'a.txt': No such file or directoryrm: cannot remove 'a.txt': No such file or directoryrm: cannot remove 'a.txt': No such file or directoryrm: cannot remove 'a.txt': No such file or directoryrm: cannot remove 'a.txt': No such file or directoryrm: 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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微店商家不发货怎么办 微店商家不退钱怎么办 维修车辆被拍违章停车怎么办 网贷申请平台太多怎么办 所在城市没有网店怎么办信用卡 拍拍贷不放款了怎么办 拍拍贷账号注销了怎么办 我在拍拍贷注销了怎么办 快贷逾期一年了怎么办 广州车牌买新车旧车怎么办 高尔夫旅行款被锁在车内怎么办 英雄联盟误删文件怎么办 拍拍贷换了号码怎么办 手机打开显示无法连接服务器怎么办 剑灵画面卡顿怎么办 cf被永久禁赛了怎么办 微信没有微游戏商店怎么办 游侠云盒下载慢怎么办2018 安卓手机玩网页游戏卡怎么办 safari点开什么都没有怎么办 康佳电视全网搜索打不开怎么办 脚被图钉扎了怎么办 电脑中毒了打不开软件怎么办 剑三程序不兼容怎么办 玩无主之地卡怎么办 平台老板跑路了怎么办 qq在苹果下载不了怎么办 下载速度快上传速度慢怎么办 苹果7开网页慢怎么办 为什么浏览器下载视频速度慢怎么办 机连WLAN网速慢怎么办 会声会影卸载后无法重新安装怎么办 电视空间不足无法卸载怎么办 堡垒之夜下载慢怎么办 手机网盘下载速度慢怎么办插件 笔记本电脑显示连接不可用怎么办 蓝魔手机充电慢怎么办 360f4手机充电慢怎么办 vivo卡了怎么办小窍门 白色衣服用84泡后变黄怎么办 用祛斑霜脱皮了怎么办