cron作业未将输出正确写入输出日志文件 [英] cron job is not writting the output to output log file correctly

查看:183
本文介绍了cron作业未将输出正确写入输出日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cron作业(一个python程序),该作业在每周的9:30 AM(EDT)开始,一直运行到4:15 PM(EDT)。该程序在下午4:15之后自动终止。我设置我的Cron工作的方式是这样的。

I have a cron job(a python program) which starts at 9:30 AM(EDT) every week day and runs till 4:15 PM(EDT). The program automatically terminates after 4:15 PM. The way i set my cron job is like this.

30 09 * * 1-5 cd /home/IBpy && python RealTimedata.py >> /home/logs/realtimedata.op 2>> /home/logs/realtimedata.er

现在,问题出在输出日志上。我的程序每分钟应该写30-40行来输出文件(realtimedata.op)。我正在使用WIN-SCP来检查我的输出日志,发现我发现cron作业是在2-3分钟后将输出日志写入日志文件,而不是在到达时立即写入日志文件。我每30秒刷新一次,并检查输出日志。
例如:-假设我将程序安排在上午9:30开始,那么仅在上午9:34之后我才能检查前3分钟的日志。

Now,the problem is with the output logs. Every minute my program is supposed to write like 30-40 lines to output file(realtimedata.op). I am using WIN-SCP to check out my output logs what i found is that the cron job is writing the output logs to log file after 2-3 minutes instead of writing as soon as it arrives. I refresh every 30 seconds and check the output log. For ex:- say suppose i schedule the program start at 9:30 AM i will be able to check first 3 minutes logs only after 9:34 AM only.

另一件事是,尽管我的程序一直成功运行到最后,但文件中仍缺少最后8-10分钟的日志数据。
我找不到原因。这是CRON的问题吗?还是我的CRON作业安排有任何问题?还是这是我用来检查日志的WIN-SCP的问题?如果这是WIN-SCP的问题,请向我建议一个比WINSCP更好的FTP客户端来检查我的文件。

And another thing is that the last 8-10 minutes log data is missing in the file though my program keeps running successfully till the end. I am unable to find the cause for this. Is this the problem of CRON? or are there any problems with my CRON job scheduling? or is this the problem of WIN-SCP which i am using to check the log? If this is the problem of WIN-SCP kindly suggest me a better FTP client than the WINSCP to check my files.

谢谢

推荐答案

到stdout的数据被缓冲,我认为是4K的块。
您可以尝试使用stdbuf或unbuffer命令,如 turn-off-缓冲

测试哪一个最合适。

The data to stdout is buffered, I think in chunks of 4K. You can try the stdbuf or unbuffer command, as explained on turn-off-buffering.
Test which one suits best.

crontab命令看起来像

The crontab command will look like

30 09 * * 1-5 cd /home/IBpy && unbuffer python RealTimedata.py >> /home/logs/realtimedata.op 2>> /home/logs/realtimedata.er

30 09 * * 1-5 cd /home/IBpy && stdbuf -i0 -o0 -e0 python RealTimedata.py >> /home/logs/realtimedata.op 2>> /home/logs/realtimedata.er

这篇关于cron作业未将输出正确写入输出日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆