[opensuse] Re: how to set global environment variables in crontabs
Meike Stone wrote:
I like to set global environment variables for all cron jobs. I see, that per default the cron runs a /bin/sh (link to /bin/bash). But in this mode of bash, the profile file will not included.
That is independent of usage of /bin/sh vs. /bin/bash. profile is never sourced in cron jobs since they are not interactive. Read http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html for more information. You might try ENV=/usr/local/etc/cron-profile in your crontab, where /usr/local/etc/cron-profile is a file that sources both /etc/profile and $HOME/.profile (or the respective bash_profiles, whatever is your goal, but then you have to set SHELL and BASH_ENV -- see the link above for the gory details). Please note that ENV=/etc/profile does not establish the same environment like login does, therefore I propose to establish a shell source script to achieve that. Joachim PS: And here's hoping that your profile doesn't do something tty related or other inherently interactive stuff. Very often, sourcing profile in cron jobs is actually a bad idea. -- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Joachim Schrod, Roedermark, Germany Email: jschrod@acm.org -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
2012/11/23 Joachim Schrod <jschrod@acm.org>:
Meike Stone wrote:
I like to set global environment variables for all cron jobs. I see, that per default the cron runs a /bin/sh (link to /bin/bash). But in this mode of bash, the profile file will not included.
http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html for more information.
Thanx for link, better than the manpage ...
You might try
ENV=/usr/local/etc/cron-profile
in your crontab, where /usr/local/etc/cron-profile is a file that sources both /etc/profile and $HOME/.profile (or the respective bash_profiles, whatever is your goal, but then you have to set SHELL and BASH_ENV -- see the link above for the gory details). Please note that ENV=/etc/profile does not establish the same environment like login does, therefore I propose to establish a shell source script to achieve that.
I tried to set up my own "script" in /etc/profile.d/my-env.sh It looks like: ============================================== export MYAPP_PATH_PREFIX="/opt/my-app" export MYAPP_LOG="$MYAPP_PATH_PREFIX/var/log" export MYAPP_APACHE_LOG="$MYAPP_PATH_PREFIX/var/log/apache2" export MYAPP_TMP="$MYAPP_PATH_PREFIX/tmp" export MYAPP_ETC="$MYAPP_PATH_PREFIX/etc" export MYAPP_LIB="$MYAPP_PATH_PREFIX/lib" ============================================== the /etc/crontab is: ============================================== SHELL=/bin/bash ENV=/etc/profile.d/my-env.sh PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin MAILTO=root # # check scripts in cron.hourly, cron.daily, cron.weekly, and cron.monthly # -*/15 * * * * root test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null 2>&1 ============================================= I set up a cronjob via /etc/cron.d/test to check the environment */1 * * * * root set >>/env.txt but only =================================== BASH=/bin/sh BASH_ARGC=() BASH_ARGV=() BASH_EXECUTION_STRING='set >>/env.txt' BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="3" [1]="2" [2]="51" [3]="1" [4]="release" [5]="x86_64-suse-linux-gnu") BASH_VERSION='3.2.51(1)-release' DIRSTACK=() EUID=0 GROUPS=() HOME=/root HOSTNAME=testhost HOSTTYPE=x86_64 IFS=' ' LOGNAME=root MACHTYPE=x86_64-suse-linux-gnu OPTERR=1 OPTIND=1 OSTYPE=linux-gnu PATH=/usr/bin:/bin POSIXLY_CORRECT=y PPID=29696 PS4='+ ' PWD=/root SHELL=/bin/sh SHELLOPTS=braceexpand:hashall:interactive-comments:posix SHLVL=1 TERM=dumb UID=0 USER=root _=/bin/sh =========================================== is shown. I started th cron via: ~# cron -nx pars,load and I see, that the variables are set: ========================================== [29969] load_database() unlinking old system_u base: system_u:load_user() linenum=1 load_env, read <SHELL=/bin/bash> load_env, <SHELL> </bin/bash> -> <SHELL=/bin/bash> linenum=2 load_env, read <ENV=/etc/profile.d/my-env.sh> load_env, <ENV> </etc/profile.d/my-env.sh> -> <ENV=/etc/profile.d/my-env.sh> linenum=3 load_env, read <PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin> load_env, <PATH> </usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin> -> <PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin> linenum=4 load_env, read <MAILTO=root> load_env, <MAILTO> <root> -> <MAILTO=root> linenum=5 linenum=6 linenum=7 linenum=8 linenum=9 load_env, read <-*/15 * * * * root test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null 2>&1> load_env, not an env var, state = 7 linenum=4 load_entry()...about to eat comments linenum=5 linenum=6 linenum=7 ==================================================== How is it possible, to set this variables global? If I look in the default crontab, delivered with SuSE, the environment variables are set in top of the file, are not inherited to the called/invoked cron.
PS: And here's hoping that your profile doesn't do something tty related or other inherently interactive stuff. Very often, sourcing profile in cron jobs is actually a bad idea.
No, only a few environment variables ... Thanx Meike -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Meike Stone writes:
2012/11/23 Joachim Schrod <jschrod@acm.org>:
You might try
ENV=/usr/local/etc/cron-profile
in your crontab, where /usr/local/etc/cron-profile is a file that sources both /etc/profile and $HOME/.profile (or the respective bash_profiles, whatever is your goal, but then you have to set SHELL and BASH_ENV -- see the link above for the gory details).
As hinted in the last cited line:
the /etc/crontab is: ============================================== SHELL=/bin/bash ENV=/etc/profile.d/my-env.sh
If $SHELL is /bin/bash, $ENV is not read. Then $BASH_ENV is read. HTH, Joachim -- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Joachim Schrod, Roedermark, Germany Email: jschrod@acm.org -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
participants (2)
-
Joachim Schrod
-
Meike Stone