Search code examples
pythongoogle-cloud-platformcrongoogle-compute-engine

crontab running python code is not saving outputs to file


I've started a compute engine instance in Google Cloud, I created a folder called "python" in the main dir, and "one.py" in that dir. Than I pasted this into one.py:

from datetime import datetime
import os
a=datetime.now()
file_to_open = os.path.join(os.getcwd(), "raw_data.txt")
with open(file_to_open, "a+") as file_object:
    file_object.seek(0)
    data = file_object.read(100)
    if len(data)>0:
        file_object.write("\n")
    file_object.write(str(a))
    file_object.close()

So far, so good. Dates are being saved to the file.

Than I added this to cronetab:

crontab -e

...
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
* * * * * python3 ~python/one.py

The cron job is working, I'm getting this outputs after running

grep CRON /var/log/syslog


Mar 21 11:33:01 instance-2 CRON[605]: (myname) CMD (python3 ~/home/python/one.py)
Mar 21 11:33:01 instance-2 CRON[604]: (CRON) info (No MTA installed, discarding output)
Mar 21 11:34:01 instance-2 CRON[647]: (myname) CMD (python3 ~/home/python/one.py)
Mar 21 11:34:01 instance-2 CRON[646]: (CRON) info (No MTA installed, discarding output)

I thought it might be sth with the root directory being in a different place so did find . raw_data.txt

there's only one, under ~python/

Can someone help me fix this? Why the cron job isn't saving the dates to the file?


Solution

  • My assumption is that it is looking for raw_data.txt in the wrong folder and failing because it cannot find the file. To see the full about of the script, add a log file for the cron job to dump the output to. You could say something like: * * * * * /use/bin/python3 /home/m/one.py >> /home/m/out.log. This would dumb the full output of the execution to out.log and give you all the information you need to solve the issue.

    Without knowing more, my assumption is that the issue is caused by os.getcwd(). This does not get the current working directory of one.py but the current working directory where cron is executed from. Since you are opening as a+, the file must exist first in order to append to the file. What you want instead is os.path.dirname(os.path.realpath(__file__)) which will give you the full directory path of one.py regardless of where the script is called from. Change that line to be this:

    file_to_open = os.path.join(os.path.dirname(os.path.realpath(__file__)), "raw_data.txt")