Where can I set environment variables that crontab will use? Where can I set environment variables that crontab will use? unix unix

Where can I set environment variables that crontab will use?


You can define environment variables in the crontab itself when running crontab -e from the command line.

LANG=nb_NO.UTF-8LC_ALL=nb_NO.UTF-8# m h  dom mon dow   command* * * * * sleep 5s && echo "yo"

This feature is only available to certain implementations of cron. Ubuntu and Debian currently use vixie-cron which allows these to be declared in the crontab file (also GNU mcron).

Archlinux and RedHat use cronie which does not allow environment variables to be declared and will throw syntax errors in the cron.log. Workaround can be done per-entry:

# m h  dom mon dow   command* * * * * export LC_ALL=nb_NO.UTF-8; sleep 5s && echo "yo"


I got one more solution for this problem:

0 5 * * * . $HOME/.profile; /path/to/command/to/run

In this case it will pick all the environment variable defined in your $HOME/.profile file.

Of course $HOME is also not set, you have to replace it with the full path of your $HOME.


Have 'cron' run a shell script that sets the environment before running the command.

Always.

#   @(#)$Id: crontab,v 4.2 2007/09/17 02:41:00 jleffler Exp $#   Crontab file for Home Directory for Jonathan Leffler (JL)#-----------------------------------------------------------------------------#Min     Hour    Day     Month   Weekday Command#-----------------------------------------------------------------------------0        *       *       *       *       /usr/bin/ksh /work1/jleffler/bin/Cron/hourly1        1       *       *       *       /usr/bin/ksh /work1/jleffler/bin/Cron/daily23       1       *       *       1-5     /usr/bin/ksh /work1/jleffler/bin/Cron/weekday2        3       *       *       0       /usr/bin/ksh /work1/jleffler/bin/Cron/weekly21       3       1       *       *       /usr/bin/ksh /work1/jleffler/bin/Cron/monthly

The scripts in ~/bin/Cron are all links to a single script, 'runcron', which looks like:

:       "$Id: runcron.sh,v 2.1 2001/02/27 00:53:22 jleffler Exp $"##       Commands to be performed by Cron (no debugging options)#       Set environment -- not done by cron (usually switches HOME). $HOME/.cronfilebase=`basename $0`cmd=${REAL_HOME:-/real/home}/bin/$baseif [ ! -x $cmd ]then cmd=${HOME}/bin/$basefiexec $cmd ${@:+"$@"}

(Written using an older coding standard - nowadays, I'd use a shebang '#!' at the start.)

The '~/.cronfile' is a variation on my profile for use by cron - rigorously non-interactive and no echoing for the sake of being noisy. You could arrange to execute the .profile and so on instead. (The REAL_HOME stuff is an artefact of my environment - you can pretend it is the same as $HOME.)

So, this code reads the appropriate environment and then executes the non-Cron version of the command from my home directory. So, for example, my 'weekday' command looks like:

:       "@(#)$Id: weekday.sh,v 1.10 2007/09/17 02:42:03 jleffler Exp $"##       Commands to be done each weekday# Update ICSCOPEn.updics

The 'daily' command is simpler:

:       "@(#)$Id: daily.sh,v 1.5 1997/06/02 22:04:21 johnl Exp $"##       Commands to be done daily# Nothing -- most things are done on weekdays onlyexit 0