How to time out a program in bash

James Michael Fultz croooow at gmail.com
Tue Oct 6 18:45:49 UTC 2009


* Kevin O'Gorman <kogorman at gmail.com> [2009-10-06 10:22 -0700]:
> I'm not sure where to ask this, but surely there are bash script
> experts watching...
> 
> I'm trying to write a bash script to test a some student programs.
> Some of them get wedged, and I can use ulimit to catch most of that.
> However, if they wedge reading stdin when they are not supposed to,
> ulimit does not catch them.
> 
> Can a bash script use an alarm?  Anybody have a quick recipe?  Or
> another approach?

#v+
timer () {
        # use 60 seconds for timeout, set accordingly
        sleep 60 && kill $$
}

# start the timer
timer & timerpid=$!

# execute whatever needs to have a timeout...

# if we've made it through, stop the timer
kill "$timerpid"
#v-




More information about the ubuntu-users mailing list