Shell scripting in cron environments
24 August 2005 13:05
I'm going to publicly humble myself here in demonstating my capability to forget that which I've learned once before, for the purpose of hopfully never forgetting it again. I've been writing some backup scripts for Oracle in Bourne shell which dynamically create backup scripts for Oracle Recovery Manager (rman), an then invoke rman, feeding it the backup script that was just created. In testing these scripts I ran into the classic problem of a script which works fine from the command-line but not when invoked from cron. The script would run,
but rman would bail with odd, unhelpful errors.
This smelled suspiciously like environment variables not being set so my first course of action was to recognize that not everything in the environment from the command-line will exist in the cron execution environment, so I dutifully set environment variables in my script to provide things like ORACLE_HOME, ORACLE_SID, and PATH:
ORACLE_HOME=/path/to/oracle/product/oravers/
ORACLE_SID=DB_SID
PATH=$PATH:$ORACLE_HOME/bin
But this still didn't fix the problem. The script would run, but rman still behaved like it wasn't seeing the environment set like it wanted. So I looked into another backup script I had written months ago, and found my problem.
You see, the environment variables I set as above were fine for anything that needed to access them within the scope of the current process. The moment I forked off another process, or ran another command (like, say, rman), those environment variables would be invisible to any child processes created by my script. How to resolve this problem? Export the variables:
ORACLE_HOME=/path/to/oracle/product/oravers/; export ORACLE_HOME
ORACLE_SID=DB_SID; export ORACLE_SID
PATH=$PATH:$ORACLE_HOME/bin; export PATH
By doing this, these variables were then visible to any child processes (like rman) that I might run from my script.
Well-seasoned shell scripters will smile at me and say, "well, duh!" In my defense, most of my scripting these days is in Perl, but at any rate, I hope that bloging about this will cement it in my mind enought to remember it 6 or 12 months down the road when I'm writing another similar script.
Comments
On 17 April 2007 07:13 nishant jindal wrote:
On 04 September 2007 02:32 Amol wrote:
|