Script exits with $? not 0 randomly, how can I see what command failed?

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 (import_catalogs_buyer.sh)
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

Assuming you have redirected output to a logfile, try changing the shebang:

#!/bin/bash -x

On all three scripts

This makes the log a lot bigger, but you can definitely see exactly where the error occurred.

May be if it suits you.. you can do the following while running script2 from script1

./script2.ksh 2>error.txt 1>output.txt

:rolleyes:
will create error.txt with stderr and output with stdout :b: