Search code examples
bashkill

Kill process after a given time bash?


I have a script that tries to make a DB connection using another program and the timeout(2.5min) of the program is to long. I want to add this functionality to the script.

If it takes longer then 5 seconds to connect, kill the process
Else kill the sleep/kill process.

The issue I'm having is how bash reports when a process is killed, that's because the processes are in the same shell just the background. Is there a better way to do this or how can I silence the shell for the kill commands?

DB_CONNECTION_PROGRAM > $CONNECTFILE &
pid=$! 
(sleep 5; kill $pid) &
sleep_pid=$!
wait $pid

# If the DB failed to connect after 5 seconds and was killed
status=$? #Kill returns 128+n (fatal error)
if [ $status -gt 128 ]; then
    no_connection="ERROR: Timeout while trying to connect to $dbserver"
else # If it connected kill the sleep and any errors collect
    kill $sleep_pid
    no_connection=`sed -n '/^ERROR:/,$p' $CONNECTFILE`
fi

Solution

  • I don't know if it's identical but I did fix a similar issue a few years ago. However I'm a programmer, not a Unix-like sysadmin so take the following with a grain of salt because my Bash-fu may not be that strong...

    Basically I did fork, fork and fork : )

    Out of memory After founding back my old code (which I amazingly still use daily) because my memory wasn't good enough, in Bash it worked a bit like this:

    commandThatMayHang.sh 2 > /dev/null 2>&1 &    # notice that last '&', we're forking
    MAYBE_HUNG_PID=$!
    sleepAndMaybeKill.sh $MAYBE_HUNG_PID 2 > /dev/null 2>&1 &   # we're forking again
    SLEEP_AND_MAYBE_KILL_PID=$!   
    wait $MAYBE_HUNG_PID > /dev/null 2>&1
    if [ $? -eq 0 ]
        # commandThatMayHand.sh did not hang, fine, no need to monitor it anymore
        kill -9 $SLEEP_AND_MAYBE_KILL 2> /dev/null 2>&1
    fi
    

    where sleepAndMaybeKill.sh sleeps the amount of time you want and then kills commandThatMayHand.sh.

    So basically the two scenario are:

    1. your command exits fine (before your 5 seconds timeout or whatever) and so the wait stop as soon as your command exits fine (and kills the "killer" because it's not needed anymore

    2. the command locks up, the killer ends up killing the command

    In any case you're guaranteed to either succeed as soon as the command is done or to fail after the timeout.