pythonsubprocesscroncasperjs

Python Subprocess returns non-zero exit status only in cron


I have a Python script that manages a series of CasperJS tasks and processes the result. It runs well from the command line, but when I run the script in cron, I get the error:

CalledProcessError: Command '['/path/to/casperjs', '/path/to/doSomething.js', 'args']' returned non-zero exit status 1

In Python, I call CasperJS:

response = subprocess.check_output(['/path/to/casperjs', '/path/to/doSomething.js', 'args'], shell=True)

I have tried shell=False and Popen as well, but I get the same result. I also tried making the entire command a string (instead of list), but that didn't help either.

Running '/path/to/casperjs /path/to/doSomething.js args' returns exit code 0 when run in the shell.

I have also added PATH=/usr/bin:/bin:/sbin:/usr/local/bin to my crontab to no avail. (As suggested in this question.)

Any ideas why I'm only getting this error in cron? Thanks!!

EDIT: In accordance with the answer below, setting shell=False and stderr=subprocess.STDOUT made everything work...


Solution

  • You should try to capture stderr in addition to stdout so that you can find out exactly why the program is failing (assuming it does indeed print some errors for you)

    cmd = ['/path/to/casperjs', '/path/to/doSomething.js', 'args']
    response = subprocess.check_output(cmd, 
                    shell=True,
                    stderr=subprocess.STDOUT)