Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    Shell script - timeout command

    General pfSense Questions
    1
    2
    1.3k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      salmonbaytech
      last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • S
        salmonbaytech
        last edited by

        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
        
        
        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.