在 CentOS 上用 nohup 启动程序时,日志和错误排查通常按下面几个思路来:
最常见写法:
nohup command > app.log 2>&1 &
含义:
> app.log:标准输出2>&1:标准错误也写进同一个文件&:后台运行如果你是这样启动的,错误就在 app.log 里
查看日志:
tail -f app.log
或翻看历史错误:
grep -i error app.log
如果你只写了:
nohup command &
CentOS 默认会把输出写到当前目录的:
nohup.out
查找:
ls -l nohup.out
cat nohup.out
tail -f nohup.out
如果当前目录没权限,可能会写到:
~/nohup.out
常见原因:
很多程序出错直接退出,不打印日志
解决:手动前台跑一次
command
看真实报错
nohup 启动的环境和 shell 不同
nohup env command > app.log 2>&1 &
或在脚本里显式 source:
nohup bash -c 'source /etc/profile; command' > app.log 2>&1 &
日志文件没写权限
touch app.log
chmod 644 app.log
看系统日志:
tail -f /var/log/messages
或
journalctl -xe
ps -ef | grep command
如果看不到进程,说明已经挂了,重点看日志或前台运行。
nohup command > /var/log/app.log 2>&1 &
或写启动脚本:
#!/bin/bash
cd /opt/app
nohup ./app > /var/log/app.log 2>&1 &
如果你愿意,可以把:
我可以帮你精确看是哪里错了。