#!/bin/bash

# Run:
# for f in `seq 1 100`; do sh timeout.sh; done | tee /tmp/log.txt
#
# Parameters
# TIMEOUT means the number of seconds that the script will wait for the program
# SIGNAL the signal emitted on the program that it should connect to abort.
# COMMAND is the command to run

TIMEOUT=9
SIGNAL=12
COMMAND="./gsttest file:///tmp/test.wav"

$COMMAND 2>&1 &
PID=$!

I=0
while [ \( `ps xa | awk '{ print $1 }' | grep -c ^$PID$` -gt 0 \) -a \( $I -lt $TIMEOUT \) ]; do
    I=$(( I + 1 ))
    sleep 1
done;

if [ `ps xa | awk '{ print $1 }' | grep -c ^$PID$` -gt 0 ]; then
    kill -$SIGNAL $PID
fi
