Instructions:
# 使用 & 使程序后台运行
python test.py &
#使用 nohup 是程序后运行
nohup python test.py
#后台执行程序 (推荐此方法)
nohup python test.py > /dev/null 2>&1 &
Description
*****************************************************************
此处的/dev/null可以替换成xxxx.log这种日志文件,可以方便后期debug
*****************************************************************
/dev/null 表示空设备文件
0 表示stdin标准输入
1 表示stdout标准输出
2 表示stderr标准错误
>/dev/null 表示将标准输出输出到/dev/null中,也就相当于 1>/dev/null
2 > error 表示将错误输出到error文件中
2>&1 也就表示将错误重定向到标准输出上
2>&1 >xxx.log :错误输出到终端,标准输出重定向到文件xxx.log中
相当于 >xxxx.log 2>&1(标准输出重定向到文件,错误重定向到标准输出)。
& 放在命令到结尾,表示后台运行,防止终端一直被某个进程占用,这样终端可以执行别到任务
配合 >xxxx.log 2>&1可以将log保存到xxxx.log文件中
但如果终端关闭,则进程也停止运行。如 command > xxxx.log 2>&1 &
nohup放在命令的开头,表示不挂起(no hang up)
所以关闭终端或者退出某个账号,进程也继续保持运行状态,一般配合&符号一起使用。如nohup command &。
The difference between nohup and &
- Reason :
&
OfSIGINT
signal immunity, forSIGHUP
not immunenohup
Of theSIGINT
signal is not immune toSIGHUP
immunity
- The result :
&
Even if you use Ctrl+, the Cprogram runs as usual, but after closing the Shell terminal, the program process disappears.hohup
After you close the Shell terminal, the program process still exists, but you use Ctrl+ Cprogram in the terminal will disappear.
- Description
-
To make the process truly unaffected by the Ctrl+ Cand shell closure in the shell, it is recommended to use
nohup command &
-
If you need to view the standard error of the intermediate print, you can use
nohup command > xxx.log 2>&1 &
-