C.J C.J - 1 month ago 8
Linux Question

Cron not running Python script if script not in home folder

I know cron is capricious and I am trying to figure out how to deal with it on linux.

I have the following

executable Python script for testing cron :

#!/usr/bin/env python

import os

f = open('test_cron_dir/test_file','w')

I added two lines to my crontab to run the script in two different folders :

* * * * * python /home/me/test_cron.py
* * * * * python /home/me/some_folder/test_cron.py

The problem is : cron runs the
script located in
but does not run the one located in
I have changed the paths in the script to absolute paths but it does not change anything to the situation. Also, I have tried to use the root crontab and it does not change anything.

Can anyone please shine the light of knowledge and experience on me ? Thanks a lot.

Answer Source

cron is running crontab(5) entries from the home directory of the user.

You need to change appropriately the directory i.e. to call the chdir(2) syscall (either thru the cd shell builtin, or inside your python script using os.chdir).

You should query the current directory (using getcwd(3), or the pwd command, or the os.getcwd in Python) in your script.

Also check your PATH if running commands.