温馨提示×

怎样定位debian nohup日志

小樊
43
2026-09-28 17:05:09
栏目: 智能运维

在 Debian 下使用 nohup 运行程序时,日志的定位主要取决于你启动时是怎么重定向的。下面按常见情况说明。


一、默认情况(最常见)

如果你是这样运行的:

nohup your_command &

那么:

  • 标准输出(stdout)和标准错误(stderr)默认都会写入当前目录的:
    nohup.out
    
  • 如果当前目录不可写,会写到:
    $HOME/nohup.out
    

查看日志:

tail -f nohup.out
# 或
less nohup.out

二、自定义日志文件(推荐)

更规范的做法是显式重定向:

nohup your_command > app.log 2>&1 &

说明:

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

日志位置:

./app.log

查看:

tail -f app.log

三、只记录错误日志

nohup your_command > /dev/null 2> error.log &

日志位置:

./error.log

四、不知道日志在哪,怎么定位?

1️⃣ 查看进程启动命令

ps -ef | grep your_command

或:

ps aux | grep your_command

你能看到完整的启动命令行,包括重定向路径。


2️⃣ 通过 /proc 查文件描述符

假设 PID 是 1234:

ls -l /proc/1234/fd

示例输出:

lr-x------ 1 user user 64 1月 10 10:00 1 -> /home/user/app.log
lr-x------ 1 user user 64 1月 10 10:00 2 -> /home/user/app.log

说明:

  • 1 = stdout
  • 2 = stderr

3️⃣ 用 lsof 查打开的文件

lsof -p 1234 | grep log

五、系统服务(不是 nohup 启动的)

如果你其实是用 systemd 启动的(不是 nohup):

journalctl -u your_service.service -f

六、实用建议 ✅

  • 生产环境不要依赖 nohup.out
  • 推荐:
    nohup cmd > /var/log/cmd.log 2>&1 &
    
  • 或直接使用 systemd / supervisor

如果你愿意,可以把你的启动命令或ps 输出贴出来,我可以帮你精确判断日志在哪。

0 踩