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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script exits with $? not 0 randomly, how can I see what command failed?
# 1  
Old 09-24-2008
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 Image

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 --[COLOR=blue! important][COLOR=blue! important]data[/COLOR][/COLOR]-binary "<commit/>" -H "Content-Type: text/[COLOR=blue! important][COLOR=blue! important]xml[/COLOR][/COLOR]; 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
# 2  
Old 09-25-2008
Assuming you have redirected output to a logfile, try changing the shebang:
Code:
#!/bin/bash -x

On all three scripts

This makes the log a lot bigger, but you can definitely see exactly where the error occurred.
# 3  
Old 09-25-2008
CPU & Memory Hmmm

May be if it suits you.. you can do the following while running script2 from script1
Code:
./script2.ksh 2>error.txt 1>output.txt

Smilie
will create error.txt with stderr and output with stdout Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script to check if files exits

Hi In live system core files are generating frequently. around 10 core files in 30 mins in root file system. which is eating my space very much below is core file core.56539 core.78886 core.12302 core.80554 core.20147 I am trying to write a script which should move... (7 Replies)
Discussion started by: scriptor
7 Replies

2. Shell Programming and Scripting

Script exits when using UNIX2dos / dos2UNIX

I'm not sure why but my script quits automatically at the point where unix2dos / dos2unix command is used. :confused::confused::confused: How do a fix it? LOG_FILE=MADDY.txt unix2dos ${LOG_FILE} exec 2> $LOG_FILE 1>&2 echo ${LOG_FILE} The script exists after the below... (3 Replies)
Discussion started by: machomaddy
3 Replies

3. Shell Programming and Scripting

Ssh bash script exits without remote command completion

Hi, My goal is to connect from unix server A to windows server B and call a bat file on windows. I am able to succeed in remoting to windows and executing a command, the issue i am facing is the shell scrip is exiting without making sure of bat file success. Can you please help me in... (4 Replies)
Discussion started by: pxp018
4 Replies

4. Shell Programming and Scripting

Execute a command after ssh exits

In my .tcshrc I define and run an alias to set my terminal title. The title contains the hostname. I do this because I ssh to other machines a lot and I always need to know which host I am typing in. The alias is defined and run in the .tcshrc like this... alias tt 'echo -n "^0;`hostname -s`^G"'... (5 Replies)
Discussion started by: salvadorjoshua
5 Replies

5. Shell Programming and Scripting

How to find whether a particular command has failed inside an sftp script?

hi, how can i know whether a command inside an sftp script has failed or not? i have a sftp expect script #!/usr/bin/expect spawn /usr/bin/sftp abc@ftp.abc.com expect "sftp>" send "cd dir\r" expect "sftp>" send "mput abc.txt\r" expect "sftp>" send "mput def.xls\r" expect "sftp>"... (5 Replies)
Discussion started by: Little
5 Replies

6. Solaris

Script redirect command output failed, why?

Hi, I put a for loop in a script to eject backup tapes from the robot. The command echo' output goes to the log file without problem, but command vmchange's output does not go to the log file although it's working fine. It still displays on the screen. I've tried '2>&1 1>$log', but nothing changed.... (5 Replies)
Discussion started by: aixlover
5 Replies

7. Shell Programming and Scripting

how to run a command as soon as user exits from session

I need to write a script, where I have to get names of files that are to be deleted from a user and have to delete those files when he exits session. How to set a particular command to be run as soon as user exits from a session? Can somebody help? I have to write a script on linux system.... (3 Replies)
Discussion started by: yashashri
3 Replies

8. Shell Programming and Scripting

'script' command exits immediately

I'm trying to capture the output of some commands with the 'script' utility. Normally, I would type 'script /path/to/output/file', then enter commands, then hit ctrl+D to end the 'script' capture. I'm having trouble with it on a server. Upon starting 'script', it exits immediately before I type... (6 Replies)
Discussion started by: jalburger
6 Replies

9. Shell Programming and Scripting

Exits from putty instead of shell script

Dear, I have written below code to initiate the log at top of my script. #Set the log file LOGFILE=<path>/<filename.log> exec > $LOGFILE 2>&1 ............... .... ... .. ............ echo -e "\n\n Script finished OK " `date "+%m/%d/%y %H:%M:%S" ` "\n\n" exit 0 the logging ends only... (14 Replies)
Discussion started by: Imran_Chennai
14 Replies

10. Shell Programming and Scripting

Rerunning a command in a script that failed?

I have a script that occasionally has a command here and there that fails and I would like to set my script up to just re run the command if the exit code is 1. Is there a simple way to do that without if/thens or redirecting to the command again? (5 Replies)
Discussion started by: trey85stang
5 Replies
Login or Register to Ask a Question