Book Reviews   Digital Libraries   Astronomy Log   Software   About  

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:
i wanna know how to set a oracle database from sybase database and how to set the environment variables

On 04 September 2007 02:32 Amol wrote:
Hi, Thanks a lot for putting up this information. Helped me for scheduling my job correctly in cron.


Happiness
True love begins when the needs of others become more important than your own.
The practice of true love begets true happiness

Me

Daniel Hanks

I'm a system administrator working for Omniture

Interested in

perl
books
python
databases
genealogy
astronomy
digital archival
digital libraries
web applications
web infrastructure
distributed storage

among other things . . .

Storyteller


Pamela Hanks

is an excellent storyteller.

(She also happens to be my wife :-)

A storyteller makes a wonderful and unique addition to family, school, church or other group events. Schedule her for your next gathering.


Kiva.org
Kiva - loans that change lives

Recent Blog Entries

Subscribe with Bloglines
- OpenWest Conference 2014 Presentation Slides - Ansible
- OpenWest Conference 2013 Presentation Slides
- Utah Open Source Conference 2012 - Presentation slides
- E-Book Review: Data Mashups in R
- Book Review: Illustrated Guide to Astronomical Wonders
- Book Review: Wicked Cool Shell Scripts
- PLUG Presentation Slides: The Open Source Data Center
- Harnessing human computational power from computer games
- I love a good roadtrip
- FamilySearch Developers Conference 2008 presentations now available online
- FHT follow up: an idea for a mobile genealogical application
- Family history and technology: it's only getting better
- President Hinckley passes away
- December is NaBoMoReMo - National Book of Mormon Reading Month
- Family History, Photos, Blogs, and Books
- The Compact Oxford English Dictionary
- 1830s English and the Book of Mormon
- Google adds My Library feature to Book Search
- Utah Open Source Conference
- Wiki diagrammer (Steal this idea!)

All Entries . . .

LDSOSS
LDS Open Source Software
A website discussing the use of Open-source software for applications useful to those sharing values of the Latter-day Saint (Mormon) faith.

© 2009, Daniel C. Hanks