4

I've got a shell script myscript.sh which calls a command, let's say mycmd. When i run that script from the terminal, e.g. ./myscript.sh, everything works fine.

But when i add that script to the crontab, the output of mycmd is empty. When i call /usr/local/bin/mycmd inside of myscript.sh everything works fine again.

So why i have to use the absolute path of the executable? Is it because it is not in the $PATH of the "cron-bash"?

1 Answer 1

6

You have it exactly right - the environment of cron may have a PATH that does not include /usr/local/bin/. You can fix this by, in your script, appending that directory to the PATH:

PATH="$PATH:/usr/local/bin/"

The best practice, though, is indeed to use explicit paths for all external binaries that a script calls just in case (for instance) a maliciously designed program conveniently called cp gets dropped into the PATH somewhere before /bin.

2
  • Thank you! But why they have varying PATHs? I thougt that the script runs "directly" from the users bash in the background. Feb 6, 2017 at 18:28
  • Cron's very limited environment is for security reasons. You can see (and fiddle with, if you like) them in /etc/default/cron.
    – DopeGhoti
    Feb 6, 2017 at 18:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .