Created
October 14, 2010 11:44
-
-
Save timuckun/626072 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DIR=$(dirname $(readlink -f $0)) | |
BINARY=${DIR}/wkhtmltoimage-i386 | |
OPTIONS='' | |
SCRIPT="${BINARY} ${OPTIONS} $1 $2" | |
TIMEOUT=$3 | |
# run $command in background, sleep for our timeout then kill the process if it is running | |
# $! has the pid of the backgrounded job | |
$SCRIPT & | |
cmd_pid=$! | |
# sleep for our timeout then kill the process if it is running | |
( sleep $TIMEOUT && kill -9 $cmd_pid && echo "ERROR - killed $command due to timeout $sleep_time exceeded" ) & | |
killer_pid=$! | |
# 'wait' for cmd_pid to complete normally. If it does before the timeout is reached, then | |
# the status will be zero. If the killer_pid terminates it, then it will have a non-zero | |
# exit status | |
wait $cmd_pid &> /dev/null | |
wait_status=$? | |
echo $wait_status | |
disown $killer_pid | |
kill -9 $killer_pid &> /dev/null | |
exit $wait_status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment