This bash script is thought to automated run processes that may not run forever but may hang sometimes. You can set a timeout after which the script will kill the processes that are still running.

# License: BSD License

# As Example we start 3 production processes in the background
# always record PID in PRODPID 

sleep 50 &

sleep 20 &

# multiple processes in a subshell as example
(sleep 20 ; sleep 10 ; sleep 5)&

export PRODPID

# record own PID
export PID=$$

# define exit function
exit_timeout() {
  echo "Timeout. These processes are not finished:"
  for i in ${PRODPID[@]} ; do
    ps -p $i |grep -v "PID TTY      TIME CMD"
    if [ $? == 0 ] ; then
      # process still alive
      echo "Sending SIGTERM to process $i"
      kill $i
  # timeout exit

# Handler for signal USR1 for the timer
trap exit_timeout SIGUSR1

# starting timer in subshell. It sends a SIGUSR1 to the father if it timeouts.
export TIMEOUT=30
(sleep $TIMEOUT ; kill -SIGUSR1 $PID) &

# record PID of timer

# wait for all production processes to finish
wait ${PRODPID[*]}

# Normal exit
echo "All processes ended normal"

# kill timer
kill $TPID