温馨提示×

centos如何重定向nohup日志

小樊
35
2026-09-15 08:05:02
栏目: 智能运维

在 CentOS(以及其他 Linux 发行版)中,nohup 通常和 & 一起使用来在后台运行程序,并重定向其输出日志。下面给你几种常见做法。


一、基本用法(重定向标准输出和错误输出)

nohup your_command > output.log 2>&1 &

说明:

  • >:重定向标准输出(stdout)
  • 2>&1:把标准错误(stderr)也重定向到 stdout
  • &:后台运行
  • 默认 nohup 会生成 nohup.out,上面写法会改用 output.log

二、使用 nohup 默认日志文件

nohup your_command &

日志会写入当前目录的:

nohup.out

如果当前目录不可写,会写到:

$HOME/nohup.out

三、只重定向错误日志(不常用)

nohup your_command 2> error.log > /dev/null &
  • 错误日志 → error.log
  • 标准输出丢弃

四、同时分离 stdout 和 stderr

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

五、追加日志(而不是覆盖)

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

>> 表示追加写入。


六、结合 tail 实时查看日志

tail -f output.log

七、推荐写法(最常用)

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

✅ 后台运行
✅ 日志追加
✅ 标准错误也记录


如果你有具体命令(如 Java、Python、Shell 脚本),我可以给你更精确的示例。

0