Shell – Environment variables are not passed to sub scripts while running on crontab

cronenvironment-variablesshell-script

I was trying to run one of the scripts (for example, wrapper.sh) which internally executes other scripts using crontab. It works fine without any issues.

When I have created a environment file (which stores the passwords-DB and paths) and trying to execute the main script (wrapper.sh) the cron entry doesn't work on those scripts which are called internally in the wrapper.

NOTE:
I tried changing PATH, added env file location before the script in cron but it still didn't work:

9 9 * * * . /$SOMEPATH/some.env; /$SOMEPATH/wrapper.sh

The environment script and wrapper.sh are in same path. when the cron runs it passes the env variables to wrapper but not to the scripts which are called internally in wrapper. Also a mail is received on /var/mail/$user saying the env file as "No such file or Directory". I've gone through many posts but couldn't figure out the resolution.

Best Answer

$SOMEPATH isn't defined within cron, so your crontab line that tries to source /$SOMEPATH/some.env will fail. (Unless //some.env exists, that is.)

For debugging cron related issues, be aware that it writes stderr messages - including its own - to the local user's mailbox. If you don't have a mail client installed (mail or mailx) you could use something like this to read these messages:

less /var/mail/$USER

Or if you want to be really generic

"${PAGER:-less}" "${MAIL:-/var/mail/${USER:-${LOGIN:-$(whoami)}}}"
Related Question