在 CentOS(以及大多数 Linux) 中,nohup 默认会把输出写到当前目录下的 nohup.out,但你可以手动重定向日志路径和类型。
下面按常见场景说明。
nohup command > app.log 2>&1 &
含义:
> app.log:标准输出写到 app.log2>&1:错误输出也合并到标准输出&:后台运行✅ 最常用
nohup command > stdout.log 2> stderr.log &
stdout.log:正常输出stderr.log:错误日志nohup command > /dev/null 2>&1 &
或:
nohup command &> /dev/null &
nohup command >> app.log 2>&1 &
适合长期运行服务,防止日志被清空。
nohup java -jar app.jar > app.log 2>&1 &
nohup python3 main.py > run.log 2>&1 &
nohup ./start.sh > start.log 2>&1 &
jobs
或:
ps -ef | grep command
你 没有重定向输出,或重定向没写全
✅ 必须显式写 > file 2>&1
可结合 logrotate,例如:
/opt/app/app.log {
daily
rotate 7
missingok
notifempty
}
如果你愿意,可以告诉我:
我可以给你一个更规范的方案(比如用 systemd 而不是 nohup)。