温馨提示×

debian nohup日志怎么导出

小樊
36
2026-10-19 04:37:27
栏目: 智能运维

在 Debian(或其他 Linux 系统)里,nohup 运行程序时,默认会把输出写到当前目录的 nohup.out 文件。如果你想知道“怎么导出日志”,一般有以下几种常见情况和做法:


一、nohup 默认日志导出(最常用)

nohup your_command > nohup.log 2>&1 &

说明:

  • >:标准输出重定向到 nohup.log
  • 2>&1:标准错误也写入同一个文件
  • &:后台运行

✅ 日志就在 nohup.log


二、分离标准输出和错误日志(推荐)

nohup your_command \
  > stdout.log \
  2> stderr.log \
  &
  • stdout.log:正常输出
  • stderr.log:错误日志

三、追加日志(不覆盖)

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

四、查看 / 导出已有 nohup.out

如果已经用默认方式运行了:

cat nohup.out

导出到其他地方:

cp nohup.out /var/log/myapp.log

或实时查看:

tail -f nohup.out

五、结合日志轮转(生产环境建议)

使用 logrotate 或 nohup + cronolog:

nohup your_command 2>&1 | cronolog /var/log/app/%Y%m%d.log &

六、查看后台任务

jobs
ps aux | grep your_command

示例完整命令

nohup python3 app.py > /var/log/app.log 2>&1 &

然后:

tail -f /var/log/app.log

如果你能告诉我:

  • 你运行的是什么程序(Python / Java / Shell)
  • 日志想导出到哪(文件 / 远程 / 系统日志)

我可以给你更精确的方案。

0 踩