Shell script - timeout command



  • any thoughts on a tool that is already installed on pfsense that will do the same thing as the timeout command?
    http://www.gnu.org/software/coreutils/manual/html_node/timeout-invocation.html

    I have a shell script, that I would like to timeout as a backup to it already exiting on its own.



  • made an SH script to accomplish what I needed. thought I would share it.

    
    #! /bin/sh
    
    timeout=$1
    sleeptime=$2
    command=$3
    
    # test pid is still around
    PIDActive() {
    	pid=$1
    
    	test=`ps -p $pid | grep $pid`
    	if [ -z "$test" ]; then
    		return 1
    	fi
    	return 0
    }
    
    # run command & capture pid
    $command&
    commandpid=$! 
    
    # What happens first? pid exits or timeout
    counter=0
    while PIDActive $commandpid && [ "$counter" -le "$timeout" ]; do
        sleep $sleeptime
        counter=`expr $counter + $sleeptime`
    done
    
    # if we get to this point and the pid is still active, kill it
    PIDActive $commandpid && kill -s KILL  $commandpid
    
    

Log in to reply