nohup命令是Linux系统中用于让程序在用户退出终端后继续运行的实用工具。下面是详细的使用方法:
nohup command [arg...] &
nohup
:保证命令不受挂断信号(SIGHUP)影响&
:将命令放入后台运行基本后台运行:
nohup python script.py &
重定向输出(推荐):
nohup python script.py > output.log 2>&1 &
> output.log
:将标准输出重定向到output.log文件2>&1
:将标准错误也重定向到标准输出(即同一个文件)jobs -l
或者查看所有进程:
ps aux | grep "script.py"
首先找到进程ID:
ps aux | grep "script.py"
然后终止进程:
kill -9 [PID]
指定输出文件:
nohup command > /path/to/output.log 2>&1 &
忽略所有输出:
nohup command > /dev/null 2>&1 &
结合timeout限制运行时间:
nohup timeout 3600 command > output.log 2>&1 &
(运行1小时后自动终止)
nohup.out
文件通过以上方法,你可以确保在退出终端后,程序仍能继续在后台运行。