温馨提示×

centos nohup日志怎么删除

小樊
46
2026-07-20 20:56:14
栏目: 智能运维

在 CentOS 下,nohup 默认会把输出写到当前目录的 nohup.out 文件里。删除或清理这个日志,常见做法如下:


一、直接删除 nohup.out(不推荐,可能影响正在运行的进程)

rm -f nohup.out

⚠️ 风险:如果程序还在运行,可能会导致进程无法再写入日志,甚至异常。


二、清空 nohup.out(推荐 ✅)

方法 1:使用 truncate

: > nohup.out

truncate -s 0 nohup.out

方法 2:使用 cat /dev/null

cat /dev/null > nohup.out

优点

  • 文件还在
  • 程序继续写日志
  • 不会中断进程

三、如果 nohup 日志输出到其他文件

比如你启动时是:

nohup java -jar app.jar > app.log 2>&1 &

清理方式:

cat /dev/null > app.log

truncate -s 0 app.log

四、防止 nohup.out 越来越大(推荐做法)

1️⃣ 启动时直接重定向到 /dev/null

nohup ./xxx.sh > /dev/null 2>&1 &

2️⃣ 使用日志切割(生产环境推荐)

例如用 logrotate

/var/log/app/*.log {
    daily
    rotate 7
    missingok
    notifempty
    compress
}

五、查看是哪个进程在写 nohup.out

lsof | grep nohup.out

✅ 总结

需求 命令
清空日志(推荐) : > nohup.out
删除日志(不推荐) rm -f nohup.out
彻底不写日志 > /dev/null 2>&1

如果你愿意,可以把你 nohup 启动命令贴出来,我可以帮你给出最合适的清理方案。

0