Coder Perfect

Where can I set environment variables that will be used by crontab?

Problem

Every hour, I execute a crontab script. The user running it has environment variables in his.bash profile that work when he runs the job from the terminal, but these are obviously ignored by crontab when it runs.

I’ve tried putting these in my.profile and.bashrc files, but they don’t appear to work. Is there a place where I can put environment variables that crontab can read?

Asked by James

Solution #1

When you execute crontab -e from the command line, you can define environment variables in the crontab itself.

LANG=nb_NO.UTF-8
LC_ALL=nb_NO.UTF-8
# m h  dom mon dow   command

* * * * * sleep 5s && echo "yo"

This option is only available in some cron implementations. Vixie-cron is now used by Ubuntu and Debian, which allows these to be stated in the crontab file (also GNU mcron).

Cronie is used by Archlinux and RedHat, and it does not allow environment variables to be defined, resulting in syntax errors in the cron.log. Per-entry workarounds are possible:

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

Answered by carestad

Solution #2

I’ve come up with another solution to this problem:

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

It will select all of the environment variables defined in your $HOME/.profile file in this scenario.

Of course, $HOME isn’t set, so you’ll have to replace it with your $HOME’s entire path.

Answered by Vishal

Solution #3

Before running the command, have cron run a shell script that establishes the environment.

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/hourly
1        1       *       *       *       /usr/bin/ksh /work1/jleffler/bin/Cron/daily
23       1       *       *       1-5     /usr/bin/ksh /work1/jleffler/bin/Cron/weekday
2        3       *       *       0       /usr/bin/ksh /work1/jleffler/bin/Cron/weekly
21       3       1       *       *       /usr/bin/ksh /work1/jleffler/bin/Cron/monthly

All of the scripts in /bin/Cron point to the same script, ‘runcron,’ which looks like this:

:       "$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/.cronfile

base=`basename $0`
cmd=${REAL_HOME:-/real/home}/bin/$base

if [ ! -x $cmd ]
then cmd=${HOME}/bin/$base
fi

exec $cmd ${@:+"$@"}

(This was written using an older coding style; currently, I’d start with a shebang ‘#!’.)

The ‘/.cronfile’ is a cron-friendly version of my profile – strictly non-interactive and no echoing for the sake of being loud. Instead, you may arrange for the.profile and other files to be executed. (The REAL HOME stuff is a byproduct of my environment; you can treat it as if it were $HOME.)

As a result, this code first reads the suitable environment before running the non-Cron version of the command from my home directory. So, for instance, here’s what 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 ICSCOPE
n.updics

The ‘daily’ command is more straightforward:

:       "@(#)$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 only

exit 0

Answered by Jonathan Leffler

Solution #4

In Ubuntu, setting vars in /etc/environment also worked for me. Variables in /etc/environment are loaded for cron as of 12.04.

Answered by Augusto Destrero

Solution #5

If you use cron to run your scripts, you can start them with:

#!/bin/bash -l

Your /.bash profile environment variables should be picked up by them.

Answered by breizhmg

Post is based on https://stackoverflow.com/questions/2229825/where-can-i-set-environment-variables-that-crontab-will-use