在 CentOS 下,nohup 默认会把输出写到当前目录的 nohup.out 文件里。删除或清理这个日志,常见做法如下:
rm -f nohup.out
⚠️ 风险:如果程序还在运行,可能会导致进程无法再写入日志,甚至异常。
truncate: > nohup.out
或
truncate -s 0 nohup.out
cat /dev/nullcat /dev/null > nohup.out
✅ 优点:
比如你启动时是:
nohup java -jar app.jar > app.log 2>&1 &
清理方式:
cat /dev/null > app.log
或
truncate -s 0 app.log
/dev/nullnohup ./xxx.sh > /dev/null 2>&1 &
例如用 logrotate:
/var/log/app/*.log {
daily
rotate 7
missingok
notifempty
compress
}
lsof | grep nohup.out
| 需求 | 命令 |
|---|---|
| 清空日志(推荐) | : > nohup.out |
| 删除日志(不推荐) | rm -f nohup.out |
| 彻底不写日志 | > /dev/null 2>&1 |
如果你愿意,可以把你 nohup 启动命令贴出来,我可以帮你给出最合适的清理方案。