nohup命令本身不直接支持超时参数,但您可以通过以下几种方法实现超时控制:
timeout 3600 nohup your_command > output.log 2>&1 &
这将使your_command在运行1小时(3600秒)后自动终止。
nohup bash -c 'your_command & sleep 3600; kill $!' > output.log 2>&1 &
#!/usr/bin/expect -f
set timeout 3600
spawn nohup your_command
expect eof
nohup timeout --preserve-status 3600 your_command > output.log 2>&1 &
--preserve-status
选项会保留被超时终止命令的退出状态码。
-k
选项调整SIGKILL的等待时间# 检查进程是否在运行
ps aux | grep your_command
# 或者查看nohup输出文件
tail -f output.log
以上方法中,方法1和方法4是最常用且可靠的实现方式。