views:

178

answers:

1

I use popen to execute commands in a Python script, and I call it via cron.
Cron calls out this script but the behavior isn't the same if I call it by hand.

Source:

from subprocess import Popen, PIPE

pp = Popen('/usr/bin/which iptables', shell=True, stdout=PIPE)

data = ''
for ln in pp.stdout:
    data = data+ln

if data == '':
    print 'ko'
else:
    print 'ok : '+data

By hand:

# python /home/user/test.py
> : /sbin/iptables

By cron (in /tmp/err_cron):

* * * * * /usr/bin/python /home/user/test.py >> /tmp/err_cron
ko
ko
ko

Why does cron not run this script normally?

+10  A: 

Normally when processes are run from cron, the PATH is set to a very restrictive value (the man page for my crontab says /usr/bin:/bin). You may need to add:

PATH=/usr/bin:/bin:/sbin

to the top of your crontab file.

Greg Hewgill
I didn't know that. That's just a generally good remark.
Paul Lammertsma
this just bit me too. thanks for the answer.
Corey Goldberg