Score:1

Cron job seems to be running but no output and got killed after a while

fr flag
VII

I am trying to run my python scripts with crontab. I have 2 python scripts in the same directory. The first one file1.py has just 1 line print(1). The second file file2.py begins with print(2) but then a long code to query and update data in my SQL database.

My setup in crontab -e is something like this (I set up the files to run every minute and save the output to cron.log

* * * * * cd /path/to/files && python3 file1.py >> /path/to/files/cron.log 2>&1
* * * * * cd /path/to/files && python3 file2.py >> /path/to/files/cron.log 2>&1

When I check the output in cron.log, at first I only see 1, which is from file1.py but I don't see any 2, the output of file2.py. However, after a while, maybe 10 minutes or so, I see Killed in cron.log.

I think the Killed message means cron started the job to run file2.py and killed the job after a while. I am not interested in the reason why cron kills my job. What I am confused is if cron started the job to run file2.py, why there is no 2 in cron.log. All I see is 1 and Killed. Both of these files run without any problem when I run them manually. Appreciate any help

VII avatar
fr flag
VII
sorry my bad, edited
Score:1
hr flag

I suspect it's because when the output is redirected, python3 buffers its standard output stream - and the buffer is not getting flushed when the process is killed.

To illustrate, given

$ cat file2.py
import time
print(2)
time.sleep(5)

then

$ timeout 3 python3 file2.py
2

whereas

$ timeout 3 python3 file2.py | cat
Terminated

If you force python3 to unbuffer its streams you will likely see the expected log output:

$ timeout 3 python3 -u file2.py | cat
2
Terminated
VII avatar
fr flag
VII
thanks for your answer, yes I need to unbuffer the output. Sorry this is my first time asking on this community so can't upvote your answer yet (not enough reputation).
mondotofu avatar
cn flag
Would it make sense to write the output to two separate cron.log files to alleviate contention?
hr flag
@mondotofu that's a good question - I really don't know
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.