Friday, October 3, 2014

CRONTAB run script or command with Environment Variables


Allow environment variable to be passed to your bash script when using crontab. This is useful for custom configs!  As my previous post about custom configs shows. This uses enviroment variable to define server configs for you scripts, allowing you to customize code/scripts per server with simple configs.

# echo enviroment variables into txt file
0 9 * * * cd /var/www/vhosts/techspecs/download; bash -l -c "`echo env` > /tmp/env.txt"
# executable file using enviroment variables
0 9 * * * cd /var/www/vhosts/techspecs/download; bash -l -c './'
# php execute using enviroment variables
0 9 * * * cd /var/www/vhosts/techspecs/download; bash -l -c 'php -f ./doit.php'


Read and execute commands from string after processing the options, then exit. Any remaining arguments are assigned to the positional parameters, starting with $0.
-c string
Equivalent to -l.