nohup 本身只是让进程忽略挂断信号并在后台运行,真正的“日志错误”通常来自程序输出或运行环境。下面按常见场景给你一套定位思路。
nohup command &
日志默认写入:
nohup.out
位置:
$HOME/nohup.out查看:
ls -l nohup.out
tail -f nohup.out
如果你这样启动:
nohup command > app.log 2>&1 &
那就看 app.log:
tail -n 200 app.log
grep ERROR app.log
ps -ef | grep your_command
或
jobs -l
netstat -tunlp | grep 端口号
# 或
ss -tunlp | grep 端口号
看日志最后几行:
tail -n 50 nohup.out
常见原因:
nohup 启动的环境 可能不是你登录 shell 的环境
排查:
nohup env > env.log 2>&1 &
对比:
echo $PATH
解决:
nohup /full/path/to/bin command > app.log 2>&1 &
或在脚本里 source ~/.bashrc
日志中出现:
Permission deniedcannot write检查:
whoami
ls -l 日志目录
如果你只写了:
nohup command > out.log &
那错误在 stderr,你看不到
✅ 正确写法:
nohup command > app.log 2>&1 &
或分开:
nohup command > app.log 2> err.log &
strace(看系统调用)strace -f -o trace.log nohup command
set -x(shell 脚本)#!/bin/bash
set -x
如果是 systemd:
journalctl -u your.service -f
nohup ./your_app > run.log 2>&1 &
echo $! > app.pid
方便:
kill $(cat app.pid)
如果你愿意,可以把:
贴出来,我可以直接帮你定位具体错误。