温馨提示×

linux nohup日志怎样重定向

小樊
44
2026-09-09 12:36:47
栏目: 智能运维

在 Linux 中,使用 nohup 运行命令时,默认会把输出写入当前目录的 nohup.out 文件。如果你想自定义日志重定向,可以用 shell 的重定向语法。


一、基础用法

1. 标准输出重定向到日志文件

nohup your_command > output.log &

2. 标准输出 + 标准错误都重定向

nohup your_command > output.log 2>&1 &

或者(等价写法,推荐):

nohup your_command &> output.log &

二、追加日志(不覆盖)

nohup your_command >> output.log 2>&1 &

>> 表示追加,> 表示覆盖


三、不记录日志(丢弃输出)

nohup your_command > /dev/null 2>&1 &

四、分别重定向 stdout 和 stderr

nohup your_command \
  > stdout.log \
  2> stderr.log &

五、结合日期命名日志(示例)

nohup your_command > app_$(date +%Y%m%d).log 2>&1 &

六、常见完整示例

nohup python app.py > app.log 2>&1 &

查看日志:

tail -f app.log

如果你有 具体命令或场景(如 Java / Python / shell 脚本),我可以给你更精确的写法。

0