nohup 命令通常会把输出重定向到当前目录下的 nohup.out 文件(或者你自己指定的日志文件)。随着程序长期运行,这个文件会越来越大,需要清理或控制大小。
下面是几种安全、常用的清理方式:
如果你用的是:
nohup your_command > nohup.out &
✅ 安全清空(推荐):
: > nohup.out
或
truncate -s 0 nohup.out
✅ 优点:
⚠️ 不要用 rm nohup.out,否则程序还在运行但日志写不进去了。
例如:
nohup your_command > app.log 2>&1 &
清空方式一样:
> app.log
或
truncate -s 0 app.log
logrotate 可以自动轮转、压缩、清理日志。
示例 /etc/logrotate.d/myapp:
/opt/myapp/nohup.out {
size 100M
rotate 5
compress
missingok
notifempty
copytruncate
}
✅ copytruncate 很关键:
nohup your_command > /dev/null 2> error.log &
nohup your_command > /dev/null 2>&1 &
ls -lh nohup.out
du -h nohup.out
清空 nohup 日志用
> nohup.out,千万别rm,长期运行用logrotate。
如果你愿意,可以告诉我:
我可以帮你写一套最合适的日志方案。