#!/bin/bash runOne() { # Create a main process group running "echo -n ''", # and a "deadman timer" process group that waits 60 seconds and # then kills the main process group. # If the main process group ends before the timer process # group, the timer process group is killed. # create a new process group so that the main job # and all of the its descendant processes can be terminated # as a whole perl -e 'setpgrp(0,0);exec(@ARGV)' bash -c "echo -n ''" & # spawn main job local pidMain=$! # pid of main job perl -e 'setpgrp(0,0);exec(@ARGV)' bash -c "sleep 60; /usr/bin/kill -9 -$pidMain" & # spawn timer job local pidTimer=$! # pid of timer job wait $pidMain # wait until main job finishes # either normally or through the timer /usr/bin/kill -9 -$pidTimer # kill the timer job } for i in 1 2 3 4 5 6 7 8 9 10; do runOne runOne runOne runOne runOne done exit 0