Debian nohup日志格式与内容详解
一 核心概念与默认行为
nohup your_command > output.log 2>&1 &
nohup your_command > output.log &
nohup your_command > /var/log/your_app.log 2>&1 &
二 日志里通常包含的内容
nohup: ignoring input and appending output to 'nohup.out'
表示忽略输入并将输出追加到指定文件。三 常见日志格式示例
仅使用 nohup(默认文件)
nohup: ignoring input and appending output to 'nohup.out'
Starting worker process [12345]
INFO 2026-01-07 10:00:01 Connected to redis://127.0.0.1:6379
ERROR 2026-01-07 10:05:12 Failed to process job: timeout
Worker exiting with status 1
说明:前两行来自 nohup 与程序启动日志;后续行格式由程序决定。
自定义日志并合并输出
[2026-01-07 10:00:00] [INFO] Service started, pid=27654
[2026-01-07 10:00:01] [DEBUG] Listening on 0.0.0.0:8080
[2026-01-07 10:10:33] [ERROR] Unhandled exception: ...
说明:通过重定向到自定义文件(如 app.log)并使用 2>&1,可将所有输出集中,便于归档与轮转。
四 查看与分析日志的实用命令
tail -f nohup.out
tail -n 200 nohup.out
grep -i "error" nohup.out
less nohup.out
ps -p <PID> -o pid,ppid,cmd,%cpu,%mem,etime
awk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' nohup.out > nohup_time.log
提示:将日志写入 /var/log/ 并使用 logrotate 做按日/按大小轮转,可避免单文件过大。五 最佳实践与排错要点
nohup your_command > /var/log/your_app.log 2>&1 &