#!/usr/bin/env bash szap=./szap-s2 # Path to szap binary szap_args="-x" # Argumets for szap channels=./channels.conf # Path to channels.conf timeout=5 # Timeout in seconds logfile=szap.log # Log file ################################################################################ function RunWithTimeout () { if [ $# -lt 2 ]; then echo "usage: $0 " return 1 fi timeout=$1 shift # Start the command. "$@" & cmdPid=$! # Start the timeout process. ( sleep $timeout; echo "$1 timed out (pid $cmdPid)"; kill $cmdPid ) & sleepPid=$! # Don't leak the children if we're interrupted by ^C etc. trap "kill $cmdPid $sleepPid; exit 5" INT TERM EXIT # Block until the command exits or times out. wait $cmdPid cmdStatus=$? # Clear out the ^C trap. trap - INT TERM EXIT if [ $cmdStatus -le 128 ] ; then # The command exited on its own; kill the timeout process. # This will kill the background shell, not the sleep # process started by it, so the sleep itself may hang around # for a while. But, since the subshell will be gone when it # exits, nothing will try to kill $cmdPid. kill $sleepPid fi # The caller can check the return value against 128 to see # if the command timed out or simply failed. return $cmdStatus } # Clear logfile rm $logfile # Channel counter i=1 while read line do printf "%03d " $i RunWithTimeout $timeout $szap $szap_args -n$i -c $channels >> logfile.log case $? in 0 ) echo -n "FE_HAS_LOCK " ;; * ) echo -n "ERROR (exit code $?) " ;; esac echo "$line" # Inc. channel counter i=$((i+1)) done < $channels