在 Debian(或其他 Linux)里,nohup 默认会把输出写到 nohup.out。清空这个日志文件有几种常见、安全的方法。
> nohup.out
或
cat /dev/null > nohup.out
✅ 优点
比如你启动时是:
nohup java -jar app.jar > app.log 2>&1 &
清空方式:
> app.log
或
truncate -s 0 app.log
truncate -s 0 nohup.out
rm nohup.out
❌ 问题:
nohup ./app.sh > app.log 2>&1 &
示例 /etc/logrotate.d/myapp:
/root/nohup.out {
size 100M
rotate 5
copytruncate
missingok
}
copytruncate 可以在不重启进程的情况下截断日志。
lsof | grep nohup.out
如果你愿意,可以告诉我:
nohup xxx & 还是 nohup xxx > file &我可以给你最合适的具体命令。