在 CentOS 下使用 nohup 时,日志保存方式主要有以下几种,最常用的是 重定向输出。
nohup command &
默认情况下:
nohup.out
⚠️ 如果当前目录不可写,会写入:
$HOME/nohup.out
nohup command > app.log 2>&1 &
说明:
>:重定向标准输出2>&1:把错误输出也写入同一个文件&:后台运行✅ 最常用方式
nohup command > stdout.log 2> stderr.log &
nohup command >> app.log 2>&1 &
nohup java -jar app.jar > app.log 2>&1 &
tail -f app.log
jobs
或
ps -ef | grep command
logrotate(生产环境推荐)示例 /etc/logrotate.d/app:
/opt/app/app.log {
daily
rotate 7
compress
missingok
notifempty
}
cronolog(按时间切分)nohup command | cronolog /opt/logs/app-%Y%m%d.log &
✅ 推荐写法:
nohup command > app.log 2>&1 &
✅ 查看日志:
tail -f app.log
如果你愿意,可以告诉我:
我可以给你更精确的示例。