Reputation: 1600
I have a Python script that works when run from the normal Terminal (and from any location) but fails when running from cron, throwing a KeyError: 'PATH'
error.
A comment on this post suggests that this is because cron runs with a different environment. When I switch using env -i /bin/bash --noprofile --norc
as suggested, the KeyError
is raised and appears to be from os.environ["PATH"]
not being set, which I can confirm.
How do I set that, and to what? Can I make this permanent for cron?
EDIT: My question is very similar to some others, but different in that it threw a specific error from Python – I think keeping this question will help if anyone gets the same KeyError
?
Upvotes: 1
Views: 1519
Reputation: 22962
You need to define your environment variables, like PATH
in your /etc/crontab
file.
Eg.:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
01 01 * * 1-5 root python /path/to/file.py
See: https://stackoverflow.com/a/2409369/1513933
Upvotes: 2