在 Linux 中使用 nohup 运行程序时,默认会把输出写到当前目录的 nohup.out 文件里。如果你想要自定义日志重定向,可以用 shell 的重定向语法。
nohup command > app.log 2>&1 &
说明:
>:重定向标准输出(stdout)2>&1:把标准错误(stderr)也重定向到 stdout&:后台运行✅ 推荐写法(最常用)
nohup command > app.out 2> app.err &
app.out:正常日志app.err:错误日志nohup command >> app.log 2>&1 &
>>:追加写入nohup command > /data/logs/app.log 2>&1 &
nohup command > /dev/null 2>&1 &
nohup python main.py > run.log 2>&1 &
查看日志:
tail -f run.log
&,否则不是后台运行2>&1 时顺序不能反2>&1 > log 是错误的nohup + &如果你用的是 Java / Python / Shell / Spring Boot,也可以告诉我,我可以给你更针对性的示例。