在 Debian(以及大多数 Linux 发行版)中,nohup 常用来让进程在终端关闭后继续运行,同时把输出记录到日志文件。
下面给你几种常见用法。
nohup your_command > app.log 2>&1 &
说明:
nohup:忽略挂断信号your_command:你要执行的命令>:重定向标准输出app.log:日志文件2>&1:把错误输出也写入同一个日志&:后台运行示例:
nohup python3 app.py > app.log 2>&1 &
nohup your_command &
日志会写到当前目录的:
nohup.out
nohup your_command > out.log 2> err.log &
或者:
nohup your_command 1> out.log 2> err.log &
nohup your_command >> app.log 2>&1 &
>> 表示追加,不会清空原日志。
nohup your_command > app_$(date +%Y%m%d).log 2>&1 &
jobs
或:
ps aux | grep your_command
如果你在 Debian 上长期运行服务,建议:
systemd(最推荐)screen / tmuxsupervisor例如 systemd 比 nohup 更稳、可开机自启、自动重启。
如果你愿意,可以告诉我:
我可以给你更合适的日志方案。