Skip to content
Advertisement

Running python script using a cron job

Ok so I am trying to run a python script every 3 hours using cron, but I think it’s not working. I know this because my script should produce some csv files and I cannot find them anywhere or the logs, plus my script is also reading from another file like (which I think is part of the issue):

with open('json_file.json') as f:
...

so if I try to run my script from a different directory with

> python ~/my_working_dir/my_script.py

I get a FileNotFoundError

FileNotFoundError: [Errno 2] No such file or directory: ‘json_file.json’

my job goes something like this:

SHELL=/usr/bin/zsh
0 */3 * * * python ~/my_working_dir/my_script.py > ~/my_working_dir/cronlogs/cron.log

I have little experience with crontab so I am not sure about the source of the problem, but my first guess is that I need to add some path somewhere

Hope someone could help

Advertisement

Answer

I think would be better if you gave open() a full path for that file. I would specify the full path to python as well.

1 */12 * * * /usr/bin/python3 /root/telesend.py >> /root/cron.log 2>&1

this is my example which is working perfectly well take a look at link for correct schedule expressions

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement