在使用 nohup 运行程序时,默认会把输出追加到当前目录的 nohup.out 文件中。如果你想关闭或避免 nohup 日志输出,可以根据需要采用以下方式:
将标准输出和标准错误都重定向到“空设备”:
nohup your_command > /dev/null 2>&1 &
说明:
> /dev/null:标准输出丢弃2>&1:标准错误也丢弃&:后台运行✅ 推荐方式,彻底关闭日志。
如果你只想不要 nohup.out,但想看错误信息:
nohup your_command > /dev/null 2> error.log &
或者:
nohup your_command 1> /dev/null &
如果程序已经在跑:
ps -ef | grep your_command
> nohup.out
/dev/nullsetsid 或 disown 替代 nohup(无日志)setsid your_command > /dev/null 2>&1 &
或:
your_command > /dev/null 2>&1 &
disown
如果是 Java / Python / Node 等:
ERROR 或 OFF| 需求 | 命令 |
|---|---|
| 完全无日志 | nohup cmd > /dev/null 2>&1 & |
| 只保留错误 | nohup cmd > /dev/null 2> err.log & |
| 清空现有日志 | > nohup.out |
如果你能告诉我具体命令或编程语言,我可以给你更精确的方案。