I have a crontab running every hour. The user running it has environment variabless in the .bash_profile
that work when the user runs the job from the terminal,
Another way - inspired by this this answer - to "inject" variables is the following (fcron example):
%daily 00 12 \
set -a; \
. /path/to/file/containing/vars; \
set +a; \
/path/to/script/using/vars
From help set
:
-a Mark variables which are modified or created for export.
Using + rather than - causes these flags to be turned off.
So everything in between set -
and set +
gets exported to env
and is then available for other scripts, etc. Without using set
the variables get sourced but live in set
only.
Aside from that it's also useful to pass variables when a program requires a non-root account to run but you'd need some variables inside that other user's environment. Below is an example passing in nullmailer vars to format the e-mail header:
su -s /bin/bash -c "set -a; \
. /path/to/nullmailer-vars; \
set +a; \
/usr/sbin/logcheck" logcheck