nohup 运行时默认会把输出写到当前目录下的 nohup.out 文件。关闭或避免产生日志,一般有几种方式,取决于你的需求。
把标准输出和错误输出都重定向到 /dev/null:
nohup your_command > /dev/null 2>&1 &
说明:
> /dev/null:标准输出丢弃2>&1:错误输出也丢弃&:后台运行✅ 不会生成 nohup.out
nohup your_command > /dev/null &
或:
nohup your_command 1>/dev/null &
nohup your_command 2>/dev/null &
nohup your_command > app.log 2>&1 &
适合需要日志但不想用 nohup.out 的情况。
nohup.out 一旦生成,不能直接“关闭”,但可以:
: > nohup.out
或:
truncate -s 0 nohup.out
gdb / fd 重定向✅ 推荐:重启程序并使用 /dev/null
setsid(替代 nohup,无 nohup.out)setsid your_command > /dev/null 2>&1 &
| 需求 | 命令 |
|---|---|
| 完全不要日志 | nohup cmd > /dev/null 2>&1 & |
| 自定义日志 | nohup cmd > app.log 2>&1 & |
| 替代 nohup | setsid cmd > /dev/null 2>&1 & |
如果你愿意,可以告诉我:
我可以给你一个更精确的示例。