[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Killing subshells
From: |
Bob Proulx |
Subject: |
Re: [Help-bash] Killing subshells |
Date: |
Tue, 30 Oct 2018 21:57:49 -0600 |
User-agent: |
Mutt/1.10.1 (2018-07-13) |
Paul Wagner wrote:
> Dear bashers,
>
> I am trying to create a simple script to schedule wget to grab radio
> programmes. Since I occasionally lose the connection, I ended up with
> something like
...
> kill %
Jesse has already pointed out that main problem you asked about.
However you are using wget and wget can retry. It might be a more
robust solution to try the wget options to retry the connection with
shorter timeouts. Perhaps something like this:
wget --retry-connrefused --waitretry=10 --read-timeout=30 --timeout=20 -t 0
Also important is to always check the exit code and if you want you
could loop and retry that upon error. Perhaps something like this:
until wget --retry-connrefused --waitretry=10 --read-timeout=30 --timeout=20 -t
0 \
-a "$name.log" -O "$name-$i.mp3" "$url"; do
sleep 30
done
Of course that loops indefinitely but exiting after a count of loops
is reasonable too.
However if you want to put things into the background then I recommend
always capturing the job id from $! into a variable. Then set up a
trap handler such that upon exit any background job can be sent the
signal. Perhaps something like this quickly created snippet:
#!/bin/sh
unset rc
cleanup() {
test -n "$rc" && kill $rc && unset rc
}
trap "cleanup" EXIT
trap "cleanup; trap - HUP; kill -HUP $$" HUP
trap "cleanup; trap - INT; kill -INT $$" INT
trap "cleanup; trap - QUIT; kill -QUIT $$" QUIT
trap "cleanup; trap - TERM; kill -TERM $$" TERM
sleep 30 &
rc=$!
sleep 10
That way an interrupt with Control-C will also send a kill to the
background task.
Hope that helps!
Bob