dvilela
dvilela

Reputation: 1262

Python subprocess call through crontab not working

I am using a python 3.6 script in a Raspberry Pi Zero W that contains the following lines:

import subprocess

result = subprocess.run(['which', 'node'],
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE)

nodeCmd = result.stdout.decode("utf-8").replace('\n', '')

print(nodeCmd)

result = subprocess.run([nodeCmd, './script.js'],
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE)

The script tries to find the node binary and make a call to a js script. When ran manually, the program works OK, but when I schedule the call through crontab, the nodeCmd variable appears blank (instead of /usr/local/bin/node) and I get the following error:

[Errno 13] Permission denied: ''

What is going on here? Is this a permissions issue?

Upvotes: 0

Views: 1350

Answers (1)

dvilela
dvilela

Reputation: 1262

So the reason seems to be that crontab has the $PATH variable set to a different value from the user $PATH. To fix it, I just had to set the wanted value in the cron file, just above the schedule lines:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Upvotes: 2

Related Questions