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.
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
# python /home/user/test.py
> : /sbin/iptables
* * * * * /usr/bin/python /home/user/test.py >> /tmp/err_cron
ko
ko
ko
Why does cron not run this script normally?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With