温馨提示×

CentOS nohup日志如何关闭输出

小樊
39
2026-08-30 13:47:10
栏目: 智能运维

在 CentOS(或其他 Linux)下,用 nohup 启动程序时,默认会把输出写到 nohup.out。如果你想关闭或丢弃输出,可以把标准输出和标准错误重定向到 /dev/null


一、完全关闭输出(推荐)

nohup your_command >/dev/null 2>&1 &

说明:

  • >/dev/null:关闭标准输出(stdout)
  • 2>&1:把标准错误(stderr)也重定向到 stdout
  • &:后台运行

✅ 这样不会生成 nohup.out


二、只关闭标准输出,保留错误日志

nohup your_command >/dev/null &

错误仍可能输出到 nohup.out


三、关闭错误输出,保留标准输出

nohup your_command 2>/dev/null &

四、已运行程序,如何停止写 nohup.out?

方法 1:清空 nohup.out(不是最佳)

: > nohup.out

truncate -s 0 nohup.out

⚠️ 仍在持续写入


方法 2:重新启动(最干净)

停止进程,用下面方式重启:

nohup your_command >/dev/null 2>&1 &

五、如果是 Java / Python / Shell 程序示例

Java

nohup java -jar app.jar >/dev/null 2>&1 &

Python

nohup python3 main.py >/dev/null 2>&1 &

Shell 脚本

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

六、查看是否生效

lsof -p PID

ps -ef | grep your_command

总结(最常用)

彻底关闭 nohup 输出:

nohup your_command >/dev/null 2>&1 &

如果你愿意,可以告诉我你运行的是 Java / Python / 自定义程序,我可以给你一个更精确、可复用的启动方案。

0