CRON problems

Question for the Unix/Network gurus here.

I want to run a python script off a cron job. I found out how to set up cron jobs with my host, and I can get it to run.

The problem is that it doesn’t seem to run from the right directory.

If I manually run the python command from the right location, it works. But if I run it from within cron, even specifying the full path (/home/myname/casualgameblogs.com/planet/planet.py), it finds the python file to run, but I think it actuall thinks it’s running in the root directory, and so it can’t find all the relative paths that the python file needs.

I tried setting up a simple secondary script as a workaround, that just had this in it (test_cd.py)
>>
cd /home/myname/casualgameblogs.com/planet
python planet.py
<<

But when I did, I got
/bin/sh: line 1: /home/myname/casualgameblogs.com/planet/phil_cd.py: Permission denied

So I guess one python file can’t run another, or I’ve got the syntax wrong or something.

Can somebody help me out? I’m a newb to both python and cron.

Try that as a shell script with this:

#!/bin/sh
cd <wherever>
python <script>

save it as whatever.sh and use chmod +x to make sure it’s executable. At that point cron should be able to run it.

Cron can be confusing because programs spawned by cron run with crons environment, not the users normal shell environment. This means that certain environment variables like PATH, LD_LIBRARY_PATH, MAILDIR, TMP, etc aren’t set.

This often leads cron newbies to frustration because their scripts run perfectly fine from shell but bomb when invoked by cron.

A few pointers for cron jobs

  1. STDOUT and STDERR probably won’t go anywhere handy or useful. So either explicitly avoid using them in your scripts or else make sure to redirect them from your cron script.

  2. The users standard variables can often be loaded by sourcing the users profile. So a shell script like:


#!/bin/sh
cd <wherever>
. /home/username/.profile
python <script>

might serve well. Substitute the proper user home directory for “/home/username” and substitute the proper profile for “.profile”. For sh it will be .profile, for ksh it will be .profile or .ksh_profile and for bash it’s usually .bash_profile

  1. Make sure the cron job is running as the same user you normally log in as. If the cron job is running as user cron or some other user then it may be necessary to grant execute permissions to your scripts to all users with chmod.

Well, I resolved in a hacky way - not by creating a mini script to modify the dir, but rather by modifying the main script change the directory to my hardcoded target right at the start with this command

os.chdir("/home/psteinx/casualgameblogs.com/planet")

You could also do something like

su - user -c ‘./script’

And then it will run your script as you. Might even be a better idea if the script was made to run as you. Though if it’s not writing anything to disk, but maybe instead a database, running it as cron is good too, just make sure your modules are accessible by cron :).