Where can I set environment variables that crontab will use?
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/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
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/.cronfile
base=`basename $0`
cmd=${REAL_HOME:-/real/home}/bin/$base
if [ ! -x $cmd ]
then cmd=${HOME}/bin/$base
fi
exec $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 ICSCOPE
n.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 only
exit 0
How to simulate the environment cron executes a script with?
Add this to your crontab (temporarily):
* * * * * env > ~/cronenv
After it runs, do this:
env - `cat ~/cronenv` /bin/sh
This assumes that your cron runs /bin/sh, which is the default regardless of the user's default shell.
Footnote: if env
contains more advanced config, eg PS1=$(__git_ps1 " (%s)")$
, it will error cryptically env: ": No such file or directory
.
crontab not getting my current envrioment variables
You can assign your environment variables into the crontab environment file (crontab -e
) so I convert all my environment variables into an env.txt
and merge the crontab environment file into that env.txt
file and replace the crontab environment file with my env.txt
.
crontab -e
default:
0 */24 * * * /usr/local/bin/python /app/manage.py crontab run 5d758d881d8f93b61f4d1b6a43eb72f6 >> /cron/django_cron.log 2<&1 # django-cronjobs for app
if you assign some VARIABLE in that file like below so the crontab will get that environment.
VARIABLE=value
0 */24 * * * /usr/local/bin/python /app/manage.py crontab run 5d758d881d8f93b61f4d1b6a43eb72f6 >> /cron/django_cron.log 2<&1 # django-cronjobs for app
here is the bash command to achieve it
$ printenv > env.txt # this will add all your environment variables in to env.txt
$ cat /var/spool/cron/crontabs/root >> env.txt # this will merge crontab environment file with your env.txt
$ cat env.txt > /var/spool/cron/crontabs/root # this will replace the crontab environment file with your env.txt
that's the only solution I produce on my own, hopefully, it will help you guys. thanks
How to use env .bashrc variables on crontab?
My favorite solution comes from Augusto Destrero, who provided the following answer to a very similar question:
Setting vars in
/etc/environment
also worked for me in Ubuntu. As of 12.04, variables in /etc/environment are loaded for cron.
Some people suggest to do a simple env >> /etc/environment
to append all your environment variables, but I would be careful with that and rather review /etc/environment
manually.
Related Topics
CSS Problems with Flask Web App
Ignore Case in Glob() on Linux
Get File Creation Time with Python on Linux
Writing to Shared Memory in Python Is Very Slow
Runtimeerror: Invalid Display Variable
How to Run Multiple Subprocesses via Fork()
Why am I Getting Socket.Gaierror: [Errno -2] from Python Httplib
List All Currently Open File Handles
Importerror: No Module Named 'Pysqlite2'
How to Use CSS Selectors to Retrieve Specific Links Lying in Some Class Using Beautifulsoup
Python Multiprocessing Memory Usage
Python's Equivalent for R's Dput() Function
/Usr/Bin/Env: Python2: No Such File or Directory
How to Get Each Dependent Command Execution Output Using Paramiko Exec_Command