Skip to content

Instantly share code, notes, and snippets.

@timuckun
Created Oct 14, 2010
Embed
What would you like to do?
#!/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