问题
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
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.
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.
Thanks
回答1:
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.
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
or
30 09 * * 1-5 cd /home/IBpy && stdbuf -i0 -o0 -e0 python RealTimedata.py >> /home/logs/realtimedata.op 2>> /home/logs/realtimedata.er
来源:https://stackoverflow.com/questions/29469117/cron-job-is-not-writting-the-output-to-output-log-file-correctly