LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-24-2008, 12:22 PM   #1
Trailsmoke
LQ Newbie
 
Registered: Sep 2008
Posts: 7

Rep: Reputation: 0
Log what exits in bash script. What causes exit code thats not 0?


Hi!
I have this situation with 3 shellscripts.

One is a "startscript" that simply calls other scripts. This one is scheduled with cron to run at regular intervals. That script runs what I'll refer to as Script 1.

Script 1 in turn runs script 2.
Sometimes, seemingly random script 2 fails and sends back an exitcode to script 1.

So how can I modify the scripts so that when script 1 is run, all stdout/stderr messages from script 2 are logged to file?

All I really need to know is what command in script 2 that doesn't exit with $?0 , and since it's random it would be good to have it in a file.

Help appreciated

SCRIPT 1
----------------------------------------------

#!/bin/bash

basedir=`dirname $0`

cd $basedir

exitcode=0

checkfail()
{
if [ $1 -gt 0 ]; then
logger_error Subprocess $pid failed. Signalling to higher level. Check import.log
if [ $exitcode -eq 0 ]; then
exitcode=$FATAL
fi
fi
return $1
}

# find and source log4sh
if [ -r "$basedir/log4sh" ]; then
log4shDir=$basedir
elif [ -r "./log4sh" ]; then
log4shDir=.
else
echo "fatal: could not find log4sh" >&2
exit 2
fi
. $log4shDir/log4sh

# Load properties
. $basedir/do_all.properties

logger_info Searching for new catalog files to import in $importCatalogsDir

# Get all direct subdirectories omitting dirs starting with .
dirs=`find $importCatalogsDir -maxdepth 1 -mindepth 1 -type d ! -name ".*"`

count=0

for d in $dirs
do
sleep 4
./import_catalogs_buyer.sh $d &
pids[$count]=$!
logger_info Started processing catalogs in $d with process PID ${pids[$count]}
let "count=$count+1"
done

for pid in "${pids[@]}"
do
sleep 2
logger_info Waiting for PID $pid to finish
wait $pid
checkfail $?
logger_info PID $pid finished
done

logger_info Done
exit $exitcode

------------------------------------------
SCRIPT 2
------------------------------------------

#!/bin/bash

basedir=`dirname $0`

exitcode=0

checkfail()
{
if [ $1 -gt 0 ]; then
logger_error Loading $f failed. Signalling to higher level. Check import.log
if [ $exitcode -eq 0 ]; then
exitcode=$FATAL
fi
fi
return $1
}

load_catalog_dir()
{
catdir=$1
catdirbase=`basename $1`
logger_info Looking for new catalogs in $catdir for customer $catdirbase
files=`find $catdir -maxdepth 1 -type f | sort`

URL=`echo $CATALOG_URL_UPDATE_TEMPLATE | sed -e s/XXXX/$catdirbase/`

for f in $files
do
logger_info Loading $f
groovy ImportSolr.groovy $f $URL $EXCHANGERATE_URL/select $tempDir
checkfail $?

if [ $? -eq 0 ]; then
HASLOADEDCATS=1

logger_info Committing $f
curl $CURLPARAMS $URL --data-binary "<commit/>" -H "Content-Type: text/xml; charset=UTF-8"
checkfail $?

#logger_info Archive $f to $catdir/done
#filebase=`basename $f`
#zip -jm $catdir/done/$filebase.zip $f
#checkfail $?
logger_info Moving $f to done directory.
mv -f $f $catdir/done/
checkfail $?
else
logger_info Moving $f to failed directory.
mv -f $f $catdir/failed/
exit $exitcode
fi
done

logger_info $catdir Done
}

# find and source log4sh
if [ -r "$basedir/log4sh" ]; then
log4shDir=$basedir
elif [ -r "./log4sh" ]; then
log4shDir=.
else
echo "fatal: could not find log4sh" >&2
exit 2
fi
. $log4shDir/log4sh

# Load properties
. $basedir/do_all.properties

HASLOADEDCATS=0

logger_info Loading catalogs from $1

load_catalog_dir $1

if [ $HASLOADEDCATS -eq 1 ]; then
# We optimize only when we have catalogs loaded
logger_info Optimizing index for customer $catdirbase
curl $CURLPARAMS $URL --data-binary "<optimize/>" -H "Content-Type: text/xml; charset=UTF-8"
checkfail $?
fi

logger_info Done loading catalogs from $1
exit $exitcode
 
Old 09-24-2008, 07:05 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
1. in a couple of places you do

if [ $exitcode -eq 0 ]; then
exitcode=$FATAL

but you haven't set a value for FATAL anywhere ..... also in Unix, zero is usually the OK/Good status, so why would you call it FATAL ?

2. You haven't actually said so, but I'm guessing

./import_catalogs_buyer.sh $d &

is the call to script2.

You could accumulate stdout/stderr msgs thus:

./import_catalogs_buyer.sh $d >>import_catalogs_buyer.log 2>&1 &

or one file per for easier debug

./import_catalogs_buyer.sh $d >import_catalogs_buyer_${d}.log 2>&1 &

You might also want to add

set -x

at the top of your script: http://tldp.org/LDP/abs/html/options.html
 
Old 09-25-2008, 03:07 AM   #3
Trailsmoke
LQ Newbie
 
Registered: Sep 2008
Posts: 7

Original Poster
Rep: Reputation: 0
I'll try this.

And yes, the script getting called is ./import_catalogs_buyer.sh

Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script for server log (namely var/log/messages) tenaciousbob Programming 17 05-24-2007 10:43 AM
what is the script which runs at the exit bash shell supersubu123 Linux - General 6 01-24-2007 12:25 PM
Bash Script- command on exit PDock Programming 6 12-23-2005 07:03 PM
Flagging exits in Bash Script dtheorem Programming 5 11-08-2003 10:15 PM


All times are GMT -5. The time now is 11:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration