Force wget to timeout
Easiest way is to use the timeout(1)
command, part of GNU coreutils, so available pretty much anywhere bash is installed:
timeout 60 wget ..various wget args..
or if you want to hard-kill wget if its running too long:
timeout -s KILL 60 wget ..various wget args..
You can run the wget command as a background process and send a SIGKILL to forcibly kill it after sleeping for a certain amount of time.
wget ... &
wget_pid=$!
counter=0
timeout=60
while [[ -n $(ps -e) | grep "$wget_pid") && "$counter" -lt "$timeout" ]]
do
sleep 1
counter=$(($counter+1))
done
if [[ -n $(ps -e) | grep "$wget_pid") ]]; then
kill -s SIGKILL "$wget_pid"
fi
Explanation:
wget ... &
- the&
notation at the end runs the command in the background as opposed to the foregroundwget_pid=$!
-$!
is a special shell variable that contains the process id of the most recently executed command. Here we save it to a variable calledwget_pid
.while [[ -n $(ps -e) | grep "$wget_pid") && "$counter" -lt "$timeout" ]]
- Look for the process every one second, if it's still there, keep waiting until a timeout limit.kill -s SIGKILL "$wget_pid"
- We usekill
to forcibly kill the wget process running in the background by sending it a SIGKILL signal.