在使用 nohup 命令时,默认会将输出(包括标准输出 stdout 和标准错误 stderr)重定向到当前目录下的 nohup.out 文件。如果你希望修改 nohup 的日志路径,可以通过重定向来实现。
nohup your_command > /path/to/logfile.log 2>&1 &
说明:
>:重定向标准输出2>&1:将标准错误重定向到标准输出&:后台运行✅ 推荐方式
nohup your_command > /path/to/logfile.log &
(错误输出仍会打印到终端或丢失)
nohup your_command > /path/to/out.log 2> /path/to/err.log &
nohup java -jar app.jar > /var/log/app/app.log 2>&1 &
或者:
nohup python main.py >> /home/user/run.log 2>&1 &
(>> 表示追加写入)
⚠️ nohup 启动后,日志路径不能直接修改
如果需要更换日志路径,只能:
如果日志会变大,建议配合 logrotate 管理日志文件,而不是只依赖 nohup。
| 需求 | 命令 |
|---|---|
| 修改日志路径 | nohup cmd > path 2>&1 & |
| 追加日志 | >> |
| 分离 stdout / stderr | > out.log 2> err.log |
如果你有具体命令或场景(如 Java / Python / Shell),我可以给你更精确的示例。