Python subprocess returns nonzero exit status only in cron

I have a Python script that manages a series of CasperJS tasks and processes the result. It works well from the command line, but when I run the script in cron, I get an 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 also tried shell=False and Popen , but I get the same result. I also tried to make the whole command a string (instead of a list), but that didn't help either.

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

I 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 only get this error in cron? Thanks!!

EDIT: According to the answer below setting shell=False and stderr=subprocess.STDOUT made everything work ...

+6
source share
1 answer

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

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

Source: https://habr.com/ru/post/912698/


All Articles