crontab and binaries in /usr/local/bin

I am working in Redhat and have few programs located in folder /usr/local/bin I would like to call from crontab for root user. I thought that by putting binaries in that folder would be sufficient to call the program directly as in the shell.

Essentially I need to specifiy the folder everytime so the crontab below doesn't work

 5 9  * * 1,2,3,4,5 my_bin some_args

but I change it into

 5 9  * * 1,2,3,4,5 source ~/.bashrc; /usr/local/bin/my_bin some_args

Do you know why?

The reason why I sourced bashrc was to add some environment libraries, in particular LD_LIBRARY_PATH as my binary couldn't find some shared libraries from /usr/local/lib.


cron jobs run in a very minimal environment, and since they're executed directly by crond without a shell (unless you force one to be created), the regular shell setup never happens. There are two standardish ways to work around this. First, you can define environment variables in your crontab (note that these'll apply to all jobs -- at least, those listed after the definitions):

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

5 9  * * 1,2,3,4,5 my_bin some_args

Second, you can edit the script to be less dependent on its environment (e.g. have it define what it's going to need itself), and then use the full path in the crontab entry:

5 9  * * 1,2,3,4,5 /usr/local/bin/my_bin some_args