Dont know how best to word this other than I'm looking for the ability to set a timer, throw it in the background and then cancel it if some other actions occurs that I want to supersede it.

while true;
if [ $event == "match" ]
#do something to kill any previous sleeps from this loop
<code> #<<need help here
#set a new sleep as of now
(sleep 300; echo "action";) &
The point is I'm watching a stream of data for a particular string. IF that string was just seen, fork off this sleep and perform some action after the timer. IF that event is seen again, remove any pending sleeps, and set a new one.

The idea in a way is to have an idle timer (kind of) that basically keeps pushing back the deadline until the event hasn't actually been seen for a full 5 minutes. If that event has been seen within that 5 minutes count down, push it back, reset the count down... start over.

The reason why this sleep has to be in the background is it can't be blocking that while loop.

I wont be shocked for a second if there's a much better way to look at this other than what I have proposed. Sleep probably isn't the best way to handle this.

On another thread for a similar topic someone proposed piping one loop into another. And instead of a sleep timer compare timestamps

(while read line; do echo "time"; done) | while read action; #compare new time to previous time.. , perform action

Makes sense to me but not quite working as expected. I welcome other thoughts. I want to be able to do this for multiple events/strings and have different actions depending on the input string. I was starting to think maybe I need an in memory key-value pair. One script to update those keys and one script to iterate through them every minute. Not sure what the best solution for that idea is... memcache comes to mind but seems a bit over kill. I could even just use regular old sql, but once again seems over kill and hits disk.