使用 nohup 启动程序后,日志通常会写入你重定向的文件(如 nohup.out 或自定义日志文件)。要实时查看这些日志,常用以下几种方法:
tail -f(最常用)tail -f nohup.out
-f:持续监视文件,有新内容就立即显示nohup python app.py > app.log 2>&1 &
那就用:
tail -f app.log
tail -n 100 -f app.log # 先显示最后100行,再实时跟踪
tailf(部分系统可用)tailf app.log
tail -f 类似,但更轻量(某些发行版已不推荐)less +F(可交互查看)less +F app.log
Ctrl + C 停止跟踪,可上下翻页F 恢复实时跟踪适合日志很长、需要回看的情况。
tail -f app.log error.log
如果 nohup 没加 &:
nohup python app.py
程序会占住终端,直接就能看到输出,但不能实时“后台看”。
正确后台方式:
nohup python app.py > app.log 2>&1 &
nohup.outgrep:tail -f app.log | grep ERROR
如果你愿意,可以告诉我:
我可以给你更具体的命令。