| BASH - terminate command after timeout |
|
This script will allow a command to be run with a timeout, if it has not completed within the timeout passed to the command the process will be killed. Usage: ./cmdtimeout "ssh server2 /usr/sbin/command_that_may_hang" 5
Will then SSH into server 2, run the command_that_may_hang and timeout after 5 seconds if it has not completed. vim cmdtimeout #!/bin/bash command=$1 # run $command in background, sleep for our timeout then kill the process if it is running $command & pid=$! echo "sleep $2; kill $pid" | at now wait $pid &> /dev/null if [ $? -eq 143 ]; then echo "WARNING - command was terminated - timeout of $2 secs reached." echo fi
|