在 CentOS(以及大多数 Linux 系统)上,nohup 日志占满磁盘是一个很常见的问题,根本原因通常不是 nohup 本身,而是使用方式 + 日志写入机制导致的。
下面分层说明原因和解决办法。
当你执行:
nohup command &
如果没有重定向输出,所有:
都会写入当前目录的:
nohup.out
⚠️ 特点:
以下情况极易爆盘:
例如:
Exception in thread ...
Exception in thread ...
Exception in thread ...
这是最容易误解的情况:
rm -f nohup.out
但磁盘空间没释放!
原因:
查看方式:
lsof | grep deleted
例如:
nohup python app.py &
nohup python app.py &
多个进程同时写,日志量翻倍。
nohup.out 不在 logrotate 管理范围内,除非你手动配置。
df -h
du -sh /*
du -sh /home/*
find / -name nohup.out 2>/dev/null
nohup command > app.log 2>&1 &
或更安全:
nohup command >> app.log 2>&1 &
示例 /etc/logrotate.d/myapp:
/var/log/myapp.log {
daily
rotate 7
compress
missingok
notifempty
copytruncate
}
⚠️ copytruncate 对 nohup 程序很关键
systemctl start myapp
journalctl -u myapp
日志自动管理,不会爆盘。
nohup command > /dev/null 2>&1 &
或只保留错误:
nohup command >/dev/null 2>error.log &
先确认:
lsof | grep nohup.out
: > nohup.out
或
truncate -s 0 nohup.out
✅ 不中断进程,立即释放空间
kill -9 PID
nohup command > app.log 2>&1 &
nohup 本身不会占满磁盘,问题出在“日志没有重定向 + 没有轮转 + 程序持续输出”。
如果你愿意,可以把:
发给我,我可以帮你写一套不会爆盘的启动方案。