在 Linux 中,nohup 默认会把程序输出写入 nohup.out,如果日志太多,可以通过以下几种方式屏蔽或控制 nohup 的日志信息。
nohup your_command > /dev/null 2>&1 &
说明:
> /dev/null:屏蔽标准输出(stdout)2>&1:把错误输出(stderr)也重定向到标准输出&:后台运行✅ 完全不生成 nohup.out
nohup your_command > /dev/null &
或:
nohup your_command 1> /dev/null 2> error.log &
nohup your_command > app.log 2>&1 &
适合需要日志但不想用 nohup.out 的情况。
nohup your_command &> /dev/null &
(bash 支持 &>)
: > nohup.out
rm -f nohup.out
⚠️ 已运行的进程仍可能持有文件句柄,日志会写到“已删除但被占用”的文件里。
如果是长期服务,建议使用:
systemdscreen / tmuxlogrotate)| 目的 | 命令 |
|---|---|
| 完全屏蔽日志 | nohup cmd > /dev/null 2>&1 & |
| 只看错误 | nohup cmd > /dev/null & |
| 自定义日志 | nohup cmd > app.log 2>&1 & |
如果你有具体命令或场景(比如 Python / Java / shell 脚本),我可以给你更精确的写法。