I\'m trying to get cron to call in the correct PATHs. When I run a Python script from shell the script runs fine as it uses the PATHs set in bashrc but when I use cron all t
The simplest workaround I've found looks like this:
* * * * * root su -l -c command
This example invokes su
as root user and starts the shell with the user's full environment, including $PATH, set as if they were logged in. It works the same on different distros, is more reliable than sourcing .bashrc (which hasn't worked for me) and avoids hardcoding specific paths which can be a problem if you're providing an example or setup tool and don't know what distro or file layout on the user's system.
You can also specify the username after su
if you want a different user than root, but you should probably leave the root
parameter before su
command since this ensures su
has sufficient privileges to switch to any user you specify.