I had a problem running a script from crontab. After some research I understood the problem was because PATH parameter doesn't include /sbin.
I looked what it does include in /etc/crontab:
PATH=/sbin:/bin:/usr/sbin:/usr/bin
As a test – simple cron job to print the PATH variable:
* * * * * echo $PATH &> /root/TMP.log
the output is:
cat /root/TMP.log
/usr/bin:/bin
I don't understand this behaviour… How do I set the PATH variable..? Or better – how to add paths to it?
Best Answer
While they are similar, a user crontab (edited using crontab -e) is different from and keeps a separate path from the system crontab (edited by editing /etc/crontab).
The system crontab has 7 fields, inserting a username before the command. The user crontab, on the other hand, has only 6 fields, going directly into the command immediately after the time fields.
Likewise, the PATH in the system crontab normally includes the /sbin directories, whereas the PATH in the user crontab does not. If you want to set PATH for the user crontab, you need to define the PATH variable in the user crontab.
A simple workaround for adding your regular PATH in shell commands in cron is to have the cronjob source your profile by running bash in a login shell. for example instead of
You can instead run
That way if your profile sets the PATH or other environment variables to something special, it also gets included in your command.