Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about the %ENV hash 1

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
Morning all,

I have a question about using the %ENV hash. I have a script I wrote that runs as a cron job. While the script runs fine from the command line, it does not as a cron because it does not have all the correct shell ENV variables, in this case $ORACLE_HOME and $ORACLE_SID. What I have done in the past is create a shell script wrapper that runs my script as in:

Code:
export ORACLE_SID = mysid
export ORACLE_HOME = opt/my/oracle

# Then run my script

/local/bin/my_perl_script.pl

While this works, I would like to not have to create my wrapper and do it all in my script. I know I can get the variable in my script with:

my $ORACLE_HOME = $ENV{ORACLE_HOME};

Is that all I need to do? If so (or if not for that matter), how does the shell my script runs under get this var?

Thanks,

Nick
 
Is there some reason your Perl script can't set these vars, e.g.
Code:
$ENV{ORACLE_SID} = whatever;
$ENV{ORACLE_HOME} = 'opt/my/oracle'

 
Thanks for the reply Mike. When I print the %ENV hash, the ORACLE ENVs are there and correct, so I assume my script can see it?

The statements you have would actually change the system ENV variable would it not? as in:

Say the SID is "whatelse" normally (set in the .profile for instance).

if I ran the statement:

$ENV{ORACLE_SID} = whatever;

Would't the system SID now be whatever?
 
It will be changed only for the life of the Perl script. When the script terminates, it'll be back to "normal."

 
Thanks Mike. That brings me back to my original question though. This is what I do right now when I need to run my scripts as a cron job:

Run the Unix "env" command, pipe it to a file and put an "export" in front of each env var in the file and then run my script as the last command.

This is not a good way to do it. If nothing else, the next time a path variable changes during an application upgrade, my script no longer works if the app home changes from something like:

/opt/app56 (version number)

to

/opt/app57

The web tuts on the ENV hash do not explain this well.

As always,

Nick

I got a Biz Degree! How the h*ll did I get here?
 
your problem is not a perl issue, it is a cron issue.
cron sets only a few environment variables. perl simply takes the current environment the script is running under and puts it into %ENV.

any command/script run under cron is responsible for setting the "correct" environment variables. even us comp science types sweat missing environment at the 1st run of a cron script.

usually, i first run a test script scheduled a few minutes in the future and pipe debugging output (eg: map { print "$ENV{$_}, $_\n"}keys %ENV ) to a file - or it will be sent as mail by cron.

BTW, "export ORACLE_HOME = opt/my/oracle" is a typo?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top