On 05/14/2017 09:28 PM, Istvan Gabor wrote:
Hello:
I would like to use cron to start and stop wget commands using wget -q (quiet) and -b (background) options. For stopping the correct wget connect at given time I need to know its pid. wget -bq url runs like this:
wget -b -q -O output-file url-address Continuing in background, pid 10874.
I should somehow record the pid in a file and use it with the kill command, but I don't know how I could do it.
Any help would be appreciated.
Thanks,
Istvan
I don't think it's possible to use $! to get the background pid, because the background process is the child process of wget, and $! returns the pid of the shell's child. I'd go with extracting the pid from wget's output: #!/bin/bash error () { echo "$0: error: $@" 2>&1; exit 1; } wout="$( wget -b -q -O output-file url-address )" \ || error "wget failed (when starting in background)" printf "%s\n" "$wout" # extract pid. pid="$( sed -n '/Continuing in background, pid / { s/^.*pid //; # strip all up to "pid" s/\.$//; # strip trailinf dot p; # print line q # quit }' <<<"$wout" \ | grep . # failure when nothing is left from the above )" \ || error "extracting pid from wget output failed" printf "wget's backgroung pid: %d\n" "$pid" exit $? Have fun, Berny -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org