nohup介绍

背景

  我们通常使用&将前台任务变为后台任务执行,但是如果只是使用&,那么在突然断网或者关闭启动该任务的终端(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

<think>嗯,用户想知道如何使用nohup命令后台运行程序以及安装配置。首先,我得回顾一下nohup的基本用法。根据引用[1],nohup的基本结构是nohup Command [ Arg … ] [ & ],所以用户需要把要执行的命令放在nohup后面,然后加上&符号让它在后台运行。比如nohup python script.py &这样。 然后,用户可能关心输出日志的问题。引用[5]提到可以将输出重定向到文件,比如nohup python test.py > my_log.log &,这样日志就保存到my_log.log了。这里要注意,如果用户没有指定输出文件,默认会生成nohup.out,但有时候可能需要明确指定路径,避免权限问题或者找不到日志的情况。 接下来是安装配置的问题。根据引用[4],在Windows环境下,nohup通常不可用,但可以通过Cygwin这样的工具来模拟Linux环境。不过引用[4]也指出Cygwin可能没有完全实现nohup的功能,所以可能需要使用其他方法。但用户的问题可能更偏向于Linux系统,所以重点还是放在Linux的配置上。 用户可能还会想知道如何关闭nohup启动的进程。引用[2]提到使用kill命令,需要先找到进程ID,可以用ps或pgrep查找,然后kill -9 PID。这时候可能需要教用户如何查看进程,比如ps aux | grep '进程名'或者jobs -l查看后台任务。 另外,引用[5]还提到了结合crontab定时任务,这可能是一个进阶用法。用户如果需要在特定时间运行程序,可以配置crontab,并在命令前加上nohup和&,确保定时任务在后台持续运行。 需要注意的常见问题,比如退出终端后进程是否继续运行。根据引用[3],添加&符号才能在后台运行,而nohup本身只是忽略挂断信号,两者结合才能保证退出后程序继续执行。还有日志文件的管理,如果程序长时间运行,日志可能过大,需要定期清理或使用日志轮转工具。 用户可能遇到的错误包括忘记加&导致程序挂起,或者没有正确指定输出路径导致权限问题。比如在权限受限的目录下运行,可能无法写入nohup.out,这时候需要指定有写权限的目录。 总结回答的结构应该是先介绍基本用法,然后详细步骤,包括命令示例、日志处理、进程管理和常见问题。同时,根据用户提供的引用,确保引用正确的部分,比如关闭进程的方法引用[2],后台运行需要&引用[3]等。</think>### 如何通过`nohup`命令后台运行程序 #### 1. **基本命令格式** 使用`nohup`后台运行程序的语法为: ```bash nohup Command [参数] [输入/输出重定向] & ``` - **Command**:需要执行的程序或脚本(如`python script.py`)。 - **输入/输出重定向**:默认输出到`nohup.out`,可通过`>`指定日志文件[^5]。 - **&**:将进程放入后台运行[^3]。 #### 2. **具体步骤** **(1) 运行程序并保存日志** ```bash nohup your_command > output.log 2>&1 & ``` - `> output.log`:将标准输出重定向到`output.log`。 - `2>&1`:将标准错误也重定向到同一日志文件。 **(2) 验证进程状态** ```bash ps aux | grep your_command ``` 通过`ps`命令查看进程是否在运行。 **(3) 关闭后台进程** - 查找进程ID: ```bash ps aux | grep your_command ``` - 终止进程: ```bash kill -9 PID # 替换PID为实际进程号[^2] ``` #### 3. **特殊场景处理** - **Windows环境**:通过Cygwin模拟Linux环境,但需注意其功能限制[^4]。 - **定时任务**:结合`crontab`使用`nohup`,确保定时任务后台持续运行: ```bash crontab -e # 添加如下行(每小时运行一次) 0 * * * * nohup /path/to/your_command >> /path/to/logfile.log 2>&1 & ``` #### 4. **常见问题** - **日志文件权限**:确保目标目录有写入权限。 - **终端关闭后失效**:若未加`&`符号,程序可能随终端关闭而终止。 - **输出内容延迟**:部分程序可能因缓冲区未刷新导致日志延迟,可手动调用`flush()`函数。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值