7

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...

Community
  • 1
  • 1
arboc7
  • 5,762
  • 2
  • 27
  • 30

1 Answers1

11

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)
jdi
  • 90,542
  • 19
  • 167
  • 203
  • 1
    I'll add to this that the most common reason for "this command works when I run it manually but not under cron/supervisor/upstart/..." is an environment variable - not always PATH - is different or missing. – mattbornski Apr 08 '12 at 21:00
  • 1
    Yes, I was checking all of the environment variables, but setting `shell=False` and adding `stderr=subprocess.STDOUT` and now everything magically works?!! – arboc7 Apr 08 '12 at 21:02
  • @mattbornski: Ya I agree. Its either that, or maybe the process is having trouble running under whatever user the crontab is set up as. I didn't want to speculate too much, but rather try and figure out what the real error might be – jdi Apr 08 '12 at 21:03
  • @arboc7: It suddenly works just from adding stderr to the pipe? That doesn't make sense. It should only allow you to see stderr output to help debug. – jdi Apr 08 '12 at 21:04
  • @jdi It doesn't make sense to me either. I'm not receiving any stderr output, but it works after adding it. – arboc7 Apr 08 '12 at 21:05
  • 2
    @arboc7: Sometimes, its best not to question why, when its working. Glad I could indirectly help. – jdi Apr 08 '12 at 21:09
  • @jdi Yes, indeed. I can confirm that it does not work when I remove `stderr=subprocess.STDOUT` *ODD!* – arboc7 Apr 08 '12 at 21:12
  • It really bothers me that this was the fix, but I can confirm that the `stderr` option fixed my issue as well. The strange thing is that this has worked for months, then one day (5/19) it just stopped working. No changes to the environment. – Tim S. May 24 '16 at 20:04
  • @TimS. why does capturing stderr bother you? maybe what changed is errors being produced? – jdi May 24 '16 at 20:23
  • 1
    @jdi capturing stderr doesn't bother me -- the fact that capturing it fixes the issue is what bothers me. Doesn't seem like something that should resolve a "non-zero exit status" issue. But I'll try to not look a gift-horse in the mouth today and just roll with it. – Tim S. May 24 '16 at 20:36