#!/bin/sh # # The first argument is the time to wait, the remaining # arguments constitute the command to be issued # date uname -n #set -x #set -v set -m TimeOut=${1} myPID=$$ ThePPID=$myPID psopt='' shift 1 FullCommand=$@ LinuxTest=`uname -s | grep Linux` if [ "${LinuxTest}" != "" ] then psopt='-w' fi CommandName=`echo $@ | cut -f1 -d' '` ICommandName=${CommandName} if [ "${CommandName}" = "time" ] then shift 1 fi CommandName=${1} Command=$@ ${FullCommand} & RetCode=$? sleep 1 if [ "${ICommandName}" = "time" ] then ps_time=`ps -u $USER $psopt -o "pid,ppid,args" | \ grep " time $CommandName" | \ grep $myPID | grep -v $0 | grep -v grep` if [ "${ps_time}" = "" ] then echo ${FullCommand} echo " has terminated before timing could begin" exit fi set ${ps_time} ThePPID=${1} fi ps_info=`ps -u $USER $psopt -o 'pid,ppid,args' | grep " $Command" | \ grep ${ThePPID} | grep -v grep | grep -v $0 |grep -v " time "` if [ "${ps_info}" = "" ] then echo "ps failure" ps -u $USER -o 'pid,user,args' sleep ${TimeOut} exit ${RetCode} fi set ${ps_info} Pid=${1} Time=${TimeOut} incr=5 [ ${TimeOut} -gt 500 ] && incr=10 [ ${TimeOut} -gt 1000 ] && incr=20 while [ ${Time} -gt 0 ] do if [ "`ps -p ${Pid} $psopt -o 'pid,args' | \ grep $CommandName`" != "" ] then Time=`expr ${Time} - ${incr}` sleep ${incr} else exit ${RetCode} fi done if [ "`ps -p ${Pid} $psopt -o 'pid,args' | \ grep $CommandName`" != "" ] then echo "============================================================" echo "=" echo "= ${0}: ${Command} has exceeded " echo "=" echo "= ${TimeOut} seconds elapsed time. It is being killed." echo "=" echo "============================================================" kill -9 ${Pid} exit 3 else exit ${RetCode} fi