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
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
Environmental variables not seen in a cron
JAVA_HOME and PATH doesn't need to be set
Can you try
/usr/data/java/current/java -jar /usr/data/apps/MyProg/MyProg.jar
Python cannot access environment variables when run via Cron
After the scheduling information place . {path you want to share}
* * * * * . {path to the env file} {script to run}
Related Topics
Launching Activities Within a Tab in Android
How Does Activity.Finish() Work in Android
How to Group a 3X3 Grid of Radio Buttons
How to Make My Arrayadapter Follow the Viewholder Pattern
How to Deserialise a Subclass in Firebase Using Getvalue(Subclass.Class)
Noclassdeffounderror: Android.Support.V7.Internal.View.Menu.Menubuilder
Android Load from Url to Bitmap
How to Delete Firebase Data After "N" Days
Android: Difference Between Onintercepttouchevent and Dispatchtouchevent
How to Make This Jbutton Visible? When I Have Progressive Scan Background Jwindow()
Getting Unwanted Characters When Reading Command Output from Ssh Server Using Jsch
How to Find a Java Thread Running on Linux with Ps -Axl
Tomcat Doesn't Stop. How to Debug This
Couldn't Install Netbeans 11.3 with Java 14 Due to Error: "Unsupported Jvm Version"