#! /usr/local/bin/bash
###########################################
# Default values
#------------------------------------------
declare -i timeout=20 # default timeout in seconds.
Kill='/bin/kill'
split_line='-------------------------------------------------------------------------------'
success_msg='status OK' # that what external program should return on success
#
###########################################
ftmp="$(mktemp /tmp/$(basename $0).XXXXXXXX)"
trap "rm -f ${ftmp}" 0 1 2 5 15
ext_prg=$1
( ${ext_prg} )>${ftmp} 2>&1 & # run some program in subshell
cPID=$! # get PID of subshell
sleep 5 # wait at least 5 seconds to get time for process to start
((timeout-=5))
status=0
if [[ ${timeout} -gt 0 ]]; then
while [[ $( ps | grep -o -G "^${cPID}"; ) -eq ${cPID} && ${status} -eq 0 ]]; do
status=$((timeout-- ? 0 : 1)); sleep 1
done
fi
if [[ ${status} -eq 1 ]]; then
eval '${Kill} -9 ${cPID}' &>/dev/null; # If timeout occured - kill child process
echo "${split_line}"
echo `basename $0`": Err: Timeout occured."
echo "Process(${cPID}): ${ext_prg}"
echo "was killed because of timeout."
echo "${split_line}"
exit 1
else
echo "${split_line}"
echo -en "Process(${cPID}): ${ext_prg}\nterminate by itself successfully.\n"
rc=$(cat ${ftmp} | awk "/${success_msg}/"'{print $2}')
if [[ -z "${rc}" ]]; then
echo "${ext_prg}: ERR: no success code returned!!!"
exit 1
else
echo "${ext_prg}: return success code!!!"
fi
echo "${split_line}"
fi
exit 0
взято тут