Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find file with python script when run via cron

Tags:

python

linux

cron

I have an automated script - I mean, it runs every 10 minutes by a cronjob. The weird thing is: The file is always found and runs through it when I start the script by hand. But it gives me a lot of troubles when it runs by cron job.

these are the rights of the files:

-rw-r--r-- 1 dataloader users     181 Dec 19 12:37 Foo.after
-rwxr-xr-x 1 dataloader users   26098 Feb 16 20:56 loader.py

this is an abstract of loader.py where it checks for Foo.after:

if os.path.exists(self.customer+'.after'):
            print 'customer file exists'
            f = open(self.customer+'.after')
like image 447
Bert Desmet Avatar asked Jul 26 '26 10:07

Bert Desmet


1 Answers

The cronjob is not executing in the same directory/environment as the script.

You can address this by adjusting your cronjob:

* * * * * cd /home/yourdir; ./loader.py

OR

* * * * * /home/mc/dotasks.sh

dotasks.sh contains:

cd /home/yourdir
./loader.py
#anything else you need to do
like image 155
hexparrot Avatar answered Jul 28 '26 22:07

hexparrot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!