How to Set Environment Variables That Crontab Will Use

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 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.

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 set environment variable for my user from cronjob in debian linux?

The cron subsystem does not set environment variables. I'll bet you're setting those env vars in your ~/.bashrc. That startup script is not read by non-interactive shells such as those launched by cron. The easiest way to deal with this is to put your env vars in a separate script (I like to name it ~/.environ) then source that file in your ~/.bashrc and your cron job. For example:

* * * * * source ~/.environ; /home/jan_necinski_binance/miniconda3/bin/python gethistoricals.py >> output.txt

Variables in crontab?

To keep my crontab clean, I would just call a shell script and do the fun stuff in the script.

What is the best solution for Cron Schedule environmental variables in a Docker container?

You should use the solution described in this answer https://stackoverflow.com/a/70897876/3669093

Whenever the environment variable is changed the container is restarted and the schedule is updated.

If you need more guidance, shoot your questions.



Related Topics



Leave a reply



Submit