在 Linux 中使用 nohup
命令运行程序时,默认生成的 nohup.out
日志文件包含以下信息:
printf
、echo
、System.out.println
等)。traceback
、Java 的异常堆栈等)。stderr
会与 stdout
混合写入 nohup.out
。nohup
会断开与终端的关联,日志中不会包含终端特有的控制字符(如颜色代码、光标移动指令等)。date
命令)。程序启动成功!
Processing data...
Error: File not found at /path/to/file
Traceback (most recent call last):
File "script.py", line 10, in <module>
open("missing_file")
FileNotFoundError: [Errno 2] No such file or directory
重定向控制
stdout
和 stderr
,可显式重定向:bash
nohup command > stdout.log 2> stderr.log &
日志轮转
nohup.out
会持续增长,需定期清理或使用工具(如 logrotate
)管理。无实时输出
tail -f nohup.out
。自定义日志路径
nohup command > /custom/path.log 2>&1 &
指定日志路径。bash
nohup sh -c 'while true; do echo "$(date): $(your_command)"; done' > nohup.out 2>&1 &
logger
或 syslog
将日志写入 /var/log/
。如果需要更详细的日志管理,建议改用专业的日志工具(如 systemd
服务、supervisord
等)。