[Nagiosplug-devel] Timeouts in BASH Plugins

Richard Edward Horner rich at richhorner.com
Thu Aug 28 17:55:21 CEST 2008


Hi,

I'm having problems implementing a timeout in a BASH plugin. I know
how to implement a timeout in BASH. Basically, you setup a trap for
the USR1 signal and then start a backgrounded process in a subshell
that sleeps for $TIMEOUT seconds and then checks if the parent process
is still running. If it is, it sends the USR1 signal which is caught
and the script cleans itself up. Here is the code to do that:

function cleanup {
	exit $1
}

function timeout {
	echo "UNKNOWN - script timed out after $MAXTIME seconds."
	cleanup $STATE_UNKNOWN
}

# since we've processed the options which potentially set the timeout
limit, we can setup a timeout trap now
trap timeout USR1
	( sleep $TIMELIMIT; if [ `pgrep -U $USER -f "$SCRIPT" | grep -c ^$$$`
-gt 0 ] ; then kill -USR1 $$ ; fi; ) &

        # do script work here

trap - USR1

This works fine locally. I've used this in a number of scripts.
However, it doesn't work over NRPE. When I put this in a plugin
script, the subshell won't run in a background when invoked by NRPE.
I'll give a simpler example to show exactly the problem:

trap timeout USR1
     ( sleep 6 ; ) &
trap - USR1

If I run that chunk of code locally, its runtime is less than a
second. If I run it over NRPE, it's runtime is over 6 seconds because
it is not backgrounding the sleep call. It's as if there's a wait
somewhere (there's not).

So, my questions are:

1) Is this no-backgrounding thing an NRPE thing or is it some BASH
thing that's occurring because there's already no controlling terminal
or ???

2) Can this be made to work in BASH or do I need to use C or Perl
(both of which I know, just not nearly as well as I know BASH)?

Thanks, Rich(ard)
-- 
Richard Edward Horner
Engineer / Composer / Electric Guitar Virtuoso
http://richhorner.com




More information about the Devel mailing list