在 CentOS 中,nohup 默认会把输出写到 nohup.out 文件。如果你不想产生 nohup 日志,可以通过重定向输出来禁用或丢弃日志。
nohup your_command > /dev/null 2>&1 &
解释:
> /dev/null:丢弃标准输出2>&1:把错误输出也重定向到标准输出&:后台运行✅ 不会产生 nohup.out
nohup your_command > /dev/null &
或等价写法:
nohup your_command 2> error.log &
nohup your_command 2> /dev/null &
nohup 但自定义日志文件如果你只是不想用 nohup.out,可以指定日志文件:
nohup your_command > my.log 2>&1 &
或追加方式:
nohup your_command >> my.log 2>&1 &
一旦 nohup.out 已生成,只能:
: > nohup.out
⚠️ 不能删除 nohup.out,否则进程可能写失败。
如果是长期运行的服务,不建议用 nohup,而是用 systemd:
[Service]
StandardOutput=null
StandardError=null
| 需求 | 命令 |
|---|---|
| 完全禁用日志 | nohup cmd > /dev/null 2>&1 & |
| 只保留错误 | nohup cmd 2> err.log & |
| 自定义日志 | nohup cmd > app.log 2>&1 & |
| 清空 nohup.out | : > nohup.out |
如果你愿意,可以告诉我:
我可以给你更合适的方案 ✅