nohup 默认会把输出写到当前目录下的 nohup.out,但可以通过重定向或环境变量来修改日志路径。下面按常见场景说明。
nohup your_command > /path/to/log.log 2>&1 &
含义:
> /path/to/log.log:标准输出重定向到指定文件2>&1:错误输出也合并到标准输出&:后台运行✅ 这是最常用、最灵活的方式
nohup your_command > /path/to/out.log 2> /path/to/err.log &
默认 > 会覆盖文件,如果想追加:
nohup your_command >> /path/to/log.log 2>&1 &
tee 同时输出到屏幕和日志(调试用)nohup your_command 2>&1 | tee /path/to/log.log &
⚠️ 不能直接修改 nohup.out 路径
但可以用下面方式“补救”:
ln -sf /path/to/log.log nohup.out
⚠️ 只对新写入有效,不一定稳定
# 找到进程
ps -ef | grep your_command
# 杀掉
kill PID
# 用新日志路径启动
nohup your_command > /path/to/log.log 2>&1 &
nohup + sh 的写法nohup sh -c 'python app.py > /data/logs/app.log 2>&1' &
nohup 不会通过环境变量修改日志路径,只能靠重定向。
| 需求 | 命令 |
|---|---|
| 修改日志路径 | nohup cmd > /path/log 2>&1 & |
| 追加日志 | >> |
| 输出/错误分开 | > out.log 2> err.log |
| 已运行进程 | 只能重启 |
如果你愿意,可以告诉我:
我可以直接给你一条最合适的命令 ✅