Python issue running popen in cron

I use popen to execute commands in a Python script, and I call it through cron.
Cron calls this script, but the behavior is not the same if I call it manually.

A 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 

Hand:

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

In cron (in / tmp / err_cron):

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

Why doesn't cron run this script normally?

+7
python cron console
source share
1 answer

Usually, when processes are started from cron, PATH 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

at the top of your crontab file.

+20
source share

All Articles