Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top