温馨提示×

CentOS nohup日志如何实时查看

小樊
44
2026-08-30 13:29:09
栏目: 智能运维

在 CentOS 上,使用 nohup 启动程序时,输出默认会写入 nohup.out(或你重定向的文件)。实时查看日志常用下面几种方式:


1️⃣ 实时查看 nohup.out(最常用)

tail -f nohup.out

如果文件在其他目录:

tail -f /path/to/nohup.out

✅ 适合查看程序实时输出


2️⃣ 查看最后 N 行并实时跟踪

tail -n 100 -f nohup.out
  • -n 100:先显示最后 100 行
  • -f:持续监控新内容

3️⃣ 使用 less 实时查看(可翻页)

less +F nohup.out

操作说明:

  • Ctrl + C:暂停自动滚动
  • Shift + F:恢复实时跟踪
  • q:退出

✅ 适合日志很长的情况


4️⃣ 如果日志被重定向到其它文件

例如启动时:

nohup java -jar app.jar > app.log 2>&1 &

查看日志:

tail -f app.log

5️⃣ 查看多个日志文件

tail -f nohup.out app.log

6️⃣ 查找实时日志中的关键字

tail -f nohup.out | grep ERROR

或高亮显示:

tail -f nohup.out | grep --color ERROR

7️⃣ 查看 nohup 进程(确认是否在运行)

ps -ef | grep 程序名

或:

jobs

8️⃣ 避免 nohup.out 无限增长(推荐)

启动时:

nohup java -jar app.jar >> app.log 2>&1 &

或按天滚动(配合 logrotate):

logrotate

✅ 总结

场景 推荐命令
实时查看 tail -f nohup.out
翻页查看 less +F nohup.out
过滤关键字 tail -f nohup.out | grep ERROR
自定义日志 nohup xxx > xxx.log 2>&1 &

如果你愿意,可以把**启动命令或程序类型(Java / Python / Shell)**发给我,我可以帮你给出更专业的日志方案。

0