简体   繁体   中英

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 test_cron.py executable Python script for testing cron :

#!/usr/bin/env python

import os

os.makedirs('test_cron_dir')
f = open('test_cron_dir/test_file','w')
f.write('stuff')
f.close()

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 test_cron.py script located in /home/me/ but does not run the one located in /home/me/some_folder/ . 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.

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

You need to change appropriately the directory ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM