在 nohup 场景下,“只记录错误”通常有两层含义,取决于你想要只保留错误日志还是只向 nohup.out 里记录错误。下面分情况说明,并给出推荐做法。
nohup your_command >/dev/null 2>nohup.out &
含义:
>/dev/null:丢弃标准输出(stdout)2>nohup.out:标准错误(stderr)写入 nohup.out✅ nohup.out 里只会包含错误信息
nohup your_command >stdout.log 2>stderr.log &
stdout.log:正常日志stderr.log:错误日志如果只想看错误:
tail -f stderr.log
nohup your_command >/dev/null 2>>error.log &
>> 表示追加nohup 默认会写 nohup.out,但你可以彻底接管输出:
nohup your_command >/dev/null 2>error.log &
或:
nohup your_command >/dev/null 2>&1 &
(这个是把错误也丢掉,不推荐)
很多服务(Java / Python / Node)自己写日志文件,那就:
nohup your_command >/dev/null 2>&1 &
然后只看程序自己的 error 日志即可。
❌ 错误理解:
nohup your_command 2>error.log
nohup.out(stdout 没处理)✅ 正确:
nohup your_command >/dev/null 2>error.log &
| 需求 | 命令 |
|---|---|
| 只记录错误到 nohup.out | nohup cmd >/dev/null 2>nohup.out & |
| 错误单独文件 | nohup cmd >/dev/null 2>error.log & |
| 错误追加 | nohup cmd >/dev/null 2>>error.log & |
| 完全不要 nohup.out | nohup cmd >/dev/null 2>&1 & |
如果你愿意,可以告诉我:
我可以给你一个最合适、最干净的日志方案。