I probably read all the threads in almost all the forums for a solution to my need. I am a beginner in shell scripting and I dont have a perfect solution yet. Below is my code snippet.
idql -n $REPOSITORY_NAME.$cs -Udmadmin -P"" -R$DM_SCRIPTS/test.api > /dev/null 2>&1
if [ $? != 0 ]; then
echo " \c"
echo "ERROR: Cannot connect to: $REPOSITORY_NAME.$cs on $HOST"
else
echo " Successfully connected to: $REPOSITORY_NAME.$cs"
fi
This is from the main logic that we use for monitoring our service. But we often see our service getting hung and so the first line of the above snippet gets hung and it doesnt proceed after that. Due to this we are not able to catch this 'service hung' condition.
Most importantly we have to retain the checks for the existing conditions (specified in the if-else conditional statements) and additionally we have to be checking for the 'hung' state. If the idql command takes more than 5 seconds, we can assume that it is hung.
Could you please help me achieve this.
i dont know idql but many connectivity tools such as ssh, scp and others have timeout option in them which can be use as a leverage. can you try
idql --help
Thanks for your reply.
idql --help
syntax: idql<docbase> [-U<user>] [-D<domain>] [-P<password>] [-K<secure flag>] [ -R<input file>] [-n] [-l<error level>] [-X] [-z] [-w<display width>]
There is no in-built timeout option. We have to manually timeout the command after say 5 seconds.
i dont know any other method but this probably is worth a try. If your script is named test.sh run it like below, sleep 10 is 10 seconds after the script started, after that it will be killed. Once its killed you can be sure it timedout. lets see if other folks have more idea.
-bash-3.2$ ./test.sh & sleep 10 ;pkill test.sh
[2] 26221
[2]+ Terminated ./test.sh
-bash-3.2$
Thanks ryandegreat25.
In this case, how to capture the result of the command, i.e, if the command is not hung, how to know whether it was a success or failure.
./test.sh
if [[ $?!=0 ]] will be true even if its a usual failure condition or a service hung condition, right?
I want to be able to catch 3 different conditions separately and print error messages correspondingly.
Not sure if this is the best way to do this you might want to create a script inside a script.
if successful, pkill wont be able to kill any which will result to exit code 1 else it will be 0
-bash-3.2$ ./test.sh & sleep 10 ;pkill test.sh
[1] 28058
[1]+ Done ./test.sh
-bash-3.2$ echo $?
1
-bash-3.2$
in your case you might want to put idql in script1 and then the conditional test in script2, though im afraid it cant capture failed or success. maybe some conditional test again in script1.
Just to add the normal execution will be 0 and failure will be something else, the kill only happens if it hang so this is where the 2nd script comes in.
1 Like
Anybody has any other solutions please?
I was able to modify the solution in another forum to match my requirement.
I tested and this is perfect for me. I appreciate all your help.
#!/bin/ksh
WAITTIME=5
# run the idql command in the background, discarding any output
idql -n $REPOSITORY_NAME -Udmadmin -P"" -R"$DM_SCRIPTS/test.api" >/dev/null 2>&1 &
IDQL_PID=$!
# set up a timeout that will kill the idql command when
# $WAITTIME seconds has passed, unless it has completed before that.
(sleep $WAITTIME; kill $IDQL_PID 2>/dev/null) &
TIMEOUT_PID=$!
# wait for the idql command to either complete or get killed; read its return status
wait $IDQL_PID
RESULT=$?
# if the timeout is still running, stop it (ignore any errors)
kill $TIMEOUT_PID 2>/dev/null
# read the return status of the timeout process (we don't need it
# but running the wait function prevents it from remaining as a
# zombie process)
wait $TIMEOUT_PID
if [ $RESULT -eq 1 ];then
echo "something is wrong with $REPOSITORY_NAME, It seems to be down. Result - $RESULT"
elif [ $RESULT -eq 143 ];then
echo "Attention!!! ***$REPOSITORY_NAME seems to be HUNG*** Result - $RESULT"
else
echo "$REPOSITORY_NAME seems to be OK. Result - $RESULT"
fi
2 Likes