[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Killing subshells
From: |
Jesse Hathaway |
Subject: |
Re: [Help-bash] Killing subshells |
Date: |
Tue, 30 Oct 2018 09:41:02 -0500 |
On Tue, Oct 30, 2018 at 8:37 AM Paul Wagner <address@hidden> 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
>
> function job() {
> i=0
> while true
> do
> i=$((i+1))
> wget -a "$name.log" -O "$name-$i.mp3" "$url"
> done &
> sleep $length
> kill %
> }
>
> while true
> do
> t=$(date '+%M %H')
> while read startmin starthour length url name
> do
> [[ $t == $startmin' '$starthour ]] && job &
> done < conf-file
> sleep 60
> done
>
> Unfortunately, the kill does not end wget. What am I missing?
Paul,
This is happening because your script does not have job control
enabled, so background jobs are not started in their own process
group, but instead are part of the script's process group. I tested by
enabling job control in a script and killing the entire process group
with a negative pid to kill. With those two changes, the children are
killed:
#!/bin/bash
set -o errexit
# enable job control so we get process groups
set -m
function job() {
while true; do
sleep 9999
done &
printf 'Job: %s\n' "$!"
pstree -gp "$$"
sleep 1
kill -9 -"$!"
wait
}
if job; then
printf 'Children Left: %s\n' "$(pgrep -f 9999 | wc -l)"
fi