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

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

10 Comments

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.
Yes, I was checking all of the environment variables, but setting shell=False and adding stderr=subprocess.STDOUT and now everything magically works?!!
@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
@arboc7: Sometimes, its best not to question why, when its working. Glad I could indirectly help.
@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.
|
0

This is because when shell=True is chosen, the first arg is interpreted differently than when shell=False. In the second case, string or array is allowed, but it appears when shell is used the string form is preferred.

The docs are vague about this: "If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments."

You can remedy this EITHER by setting shell=False or by setting it to true and passing in the command as a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.