大网数据广告活动
服务器运维问答

Cron doesn't write in log instantly on Ubuntu

· 52CE 我爱测

I execute a Python script through cron . The script is containing many print statements from its beginning (first line is a print). However even if the script is running, the log file is created but remains blank. Here is my cron…

问题原文

原帖链接 · Mehdi · CC BY-SA 4.0

I execute a Python script through cron. The script is containing many print statements from its beginning (first line is a print). However even if the script is running, the log file is created but remains blank.

Here is my crontab:

00 14 * * * python DE.py >> DElog.log 2>&1

Is anyone have a clue why?

已采纳解答

原帖链接 · HBruijn · CC BY-SA 4.0

Patience is a virtue.

For starters the log file gets created first, even before the script starts running and generating output.

Second on Linux when standard output gets redirected to something other than a TTY (a terminal window), the output will be fully buffered by default, rather than line buffered.

Because you also redirect standard error to stdout (with 2>&1) no output will be written to disk until:
- either your script completes
or
- the output exceeds the default buffer size (of 4k?).

You can adjust the buffering with stdbuff and make the error and output modes unbuffered:

stdbuf -o0 -e0 python DE.py >> DElog.log 2>&1

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-05-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。