help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Cron jobs and env vars


From: Mun
Subject: Re: [Help-bash] Cron jobs and env vars
Date: Fri, 30 Nov 2012 14:08:15 -0800
User-agent: Mutt/1.5.21 (2010-09-15)

Hi Greg,

You solved my issue; but if you are curious about my answers to your
questions, see my comments below.

On Fri, Nov 30, 2012 at 05:09 AM PST, Greg Wooledge wrote:
GW> On Thu, Nov 29, 2012 at 08:05:13PM -0800, Mun wrote:
GW> > I need to run a cron job that launches a script to set up environment
GW> > variables which are needed for a successive script call.  I have
GW> > included a representation of my script below.  I am experiencing
GW> > behavior which I cannot explain with the environment variables.
GW> 
GW> I don't understand what "successive script call" means.  Do you mean
GW> that you are launching a second script as a child of the first one?
GW> Or are you exec-ing a second script to replace the first one?  Or
GW> do you mean "commands that appear later on in the first script"?

I'm basically doing this:

------------------------------ Delimiter BEGIN --------------------------------

/path/setUpEnvVars arg1 arg2 &> /home/mun/out.log <<EOF_REGR
runSuccessiveScript &> /home/mun/out2.log
exit
EOF_REGR

------------------------------- Delimiter END ---------------------------------


GW> > #! /bin/bash
GW> > 
GW> > export SHELL="/bin/bash -i"
GW> > 
GW> > /path/setUpEnvVars arg1 arg2 &> /home/mun/out.log <<EOF_REGR
GW> > 
GW> > printenv             // Correctly dumps env vars set up via setUpEnvVars
GW> > echo $VAR1           // VAR1 (set in setupEnvVars) appears unset
GW> > printenv|grep VAR1   // VAR1 is correctly printed
GW> > echo $HOME           // HOME is correctly printed
GW> > 
GW> > exit
GW> > EOF_REGR
GW> 
GW> Your here document delimiter is not quoted.  This means that bash, while
GW> processing this script, performs replacements of $VAR1 and $HOME inside
GW> the body of the here document.  If you quote the delimiter, like this:
GW> 
GW> /path/setUpEnvVars >log 2>&1 <<'EOF_REGR'
GW> echo "$VAR1"
GW> etc.
GW> EOF_REGR
GW> 
GW> then it will pass $VAR1 through to setUpEnvVars without expanding it.

Very helpful information!  I did not realize that subtlety.  In fact,
after making that change, my cron job is working :-)

GW> That said, you appear to be feeding (almost) bash commands to something
GW> called /path/setUpEnvVars.  I have no idea what this thing is.  It had
GW> better be able to read bash commands as input, but with C++ style
GW> comments, because that's what you seem to be giving it.

The C++ style comments are out of habit and I only meant that to be
documentation for the reader regarding the results of the respective
command when the script is executed.  Those comments are not in the
actual script.

Thanks very much for the help, Greg.  That really helped me out.

Happy Holidays,

-- 
Mun

GW> Also, if you intended that "setUpEnvVars" would populate the environment
GW> of bash so that you could use those variables AFTER setUpEnvVars has
GW> exited, you will be disappointed.  Since setUpEnvVars is a child process,
GW> any changes it makes to the environment are lost when it exits.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]