Need help implementing a timout in my Shell Script for RHEL6

Hey Guys,

My problem:

I have a script that will be querying the database every minute to see if it gets a response, the response its querying for is "UP" in a table i made called dbup in the database.

Now, I am trying to add the component to implement a timeout if the script does not get a response from the server and skip to next line etc.

I would like to implement it either at the DB layer because of issues we have had with the product.

We want to skip the -->

I assign the variable query_select1, before I query DB to assign it to STRING "UP.

query_select1="down"
-->query_select1= ##QUERY SELECT FROM DB String "UP"## 

Any help would be appreciated. NOTE: we would like to avoid killing the script, but if necessary I might be ok with killing it and then sending message to myself that process got killed this is why.
But I really could use a solution to this because When the variable ="down" stays the same and doesn't get changed to "UP" from Database, then it gets passed onto the auto generated emails we have setup.

If there are other methods that I haven't thought of let me know as well.

RHEL6

You can have subshells that watch each other, so if the query loop has not written a new output to the output file for N seconds, another looping watcher can decide it is hung and kill and restart it. A third looping watcher-publisher can declare it down until there is a fresh 'up', and vice versa, and publish the status and status changes.

Ideally, I would like to implement less watcher scripts as a best practice (This is an enterprise environment)

and the script I am going to implement will run every minute so it might be an unnecessary load to do what you suggest.

If I am wrong or my assumptions in correct let me know.

and FYI, the script will be scheduled to run every 1 minute, in background.
It has a LOT of temporary variables too -so the cleanup process more complicated.

---------- Post updated at 12:03 PM ---------- Previous update was at 12:00 PM ----------

also I found this googling,

is this something that might work or no?

trap 'echo script_timeout ; exit ' USR1   
wait_n_sig() {    sleep $2    [ -d /proc/$1 ] && kill -s USR1 $1 }   
timeout() {    wait_n_sig $$ $1 & }   
timeout 5 read -p "Name: " name echo "Your name is " $name

---------- Post updated at 12:06 PM ---------- Previous update was at 12:03 PM ----------

DGPICKETT, for the watcher script you describe can you expand on the logic for me I am bit of an intermediate shell guy.

like I mean if its not too much could you expand:
1.....
2.....
3....

in terms of what you mean.

Thanks!

---------- Post updated at 12:14 PM ---------- Previous update was at 12:06 PM ----------

If I implement in my subshell the script running too long kill and email its just basically this right:
-----------------------------
(
##If runs for this time
##KILL MAIN.sh
##EMAIL NOTICE
)

MAIN.SH
------------------------------

---------- Post updated at 12:46 PM ---------- Previous update was at 12:14 PM ----------

Will this do as the watcher subshell ?

( sleep 20 ) & pid=$! ( sleep 2 && kill -HUP $pid ) 2>/dev/null & watcher=$! if wait $pid 2>/dev/null; then     echo "your_command finished"     pkill -HUP -P $watcher     wait $watcher else     echo "your_command interrupted" fi