在使用 nohup 命令时,可以通过重定向的方式来自定义日志(输出)文件的路径。
nohup your_command > /path/to/logfile.log 2>&1 &
说明:
nohup:忽略挂断信号,使程序在退出终端后继续运行your_command:你要执行的命令>:标准输出重定向/path/to/logfile.log:你指定的日志文件路径2>&1:将标准错误(2)重定向到标准输出(1),即错误日志也写入同一个文件&:后台运行nohup python app.py > /home/user/logs/app.log 2>&1 &
这样:
/home/user/logs/app.lognohup your_command > /dev/null 2> /path/to/error.log &
nohup your_command > /path/to/out.log 2> /path/to/err.log &
默认 > 会覆盖原文件,如果想追加:
nohup your_command >> /path/to/logfile.log 2>&1 &
jobs
# 或
ps -ef | grep your_command
nohup 本身不能直接“设置日志路径”,而是通过 Shell 重定向 来控制日志文件位置。你只需要在命令后加上 > 路径 2>&1 即可。