[wplug] executing a python script from crontab

Patrick Wagstrom pwagstro at andrew.cmu.edu
Fri May 26 00:04:20 EDT 2006


> <snip>
> Traceback (most recent call last):
>   File "/root/scripts/boxster_school_smtp.py", line 19, in ?
>     mssg = open('mssg.txt', 'r').read()
> IOError: [Errno 2] No such file or directory: ' mssg.txt'
> </snip>

Your problem isn't with your python script at all, rather it's with the
location you're executing the script form.  One of the tricky things
with cron is that you need to make sure you're in the correct directory.
What directory does mssg.txt reside in?  If it's supposed to be in /root
then you'll want to change the line to:

*/5 * * * cd /root && /usr/local/bin/python /root/scripts/boxster_school_smtp.py

Another option might make it read in the value of sys.argv[1] for the
location of the file instead.  Then you'll call:

import sys
mssg = open(sys.argv[1], 'r').read()

And in your crontab insert something like:

*/5 * * * /usr/local/bin/python /root/scripts/boxster_school_smtp.py [LOCATION OF MSSG.TXT]

--Patrick




More information about the wplug mailing list