在使用 nohup 运行程序时,日志通常用于记录程序的输出和错误信息。如果你发现程序异常或崩溃,可以通过以下步骤来定位 nohup 日志中的错误。
nohup command > output.log 2>&1 &
或
nohup command &
默认会生成:
nohup.out
通常在当前工作目录下。
查看文件:
ls -l nohup.out
如果你自己指定了日志:
nohup python app.py > app.log 2>&1 &
则日志在 app.log。
tail -f nohup.out
# 或
tail -f app.log
tail -n 200 nohup.out
grep -i error nohup.out
grep -i exception nohup.out
grep -i "traceback" nohup.out
command > log.txt 2>&1 &
>:标准输出2>:错误输出2>&1:错误也写入同一文件如果你这样写:
nohup command > out.log 2> err.log &
错误在 err.log,更容易定位。
看日志开头:
ModuleNotFoundError
No such file or directory
Permission denied
看 最后几行:
tail -n 50 nohup.out
重点看:
Traceback (most recent call last):
从下往上读错误。
看:
Exception in thread "main"
Caused by:
ps -ef | grep your_command
或
jobs -l
如果进程不在,但日志有错误 → 程序已退出。
python app.py
看是否直接报错。
nohup command >> app.log 2>&1 &
>> 避免覆盖旧日志。
# 1. 查看进程
ps -ef | grep myapp
# 2. 查看日志
tail -f nohup.out
# 3. 搜索错误
grep -i error nohup.out
# 4. 前台测试
./myapp
如果你愿意,可以:
我可以帮你直接分析具体错误原因。