linux - Bash redirect vs. pipe -
i have following bash script:
#!/bin/bash trap "trap - sigterm && kill -- -$$" sigint sigterm proxy_list="${1}" url="google.com" pool=5 timeout=2 check_proxy() { local socks_proxy="${1}" retry in {0..2}; time_connect="$(curl ${url} --socks5 ${socks_proxy} -m${timeout} -o /dev/null -s -w %{time_connect})" if [ $? -eq 0 ]; echo "${socks_proxy} (${time_connect}s) retries=${retry}" break fi done } while read proxy; while true; if [ "$(jobs -rp | wc -l)" -lt "${pool}" ]; check_proxy "${proxy}" & break fi wait -n done done < "${proxy_list:-/dev/stdin}" wait
the script either use filename (${1}
) input, or stdin if no positional parameter provided.
(pipe) if run script follows , try kill ctrl+c during runtime:
cut -f1 -d' ' data/socks_tested_sorted.list | ./curl_socks_tester.sh
i error , script continues running:
./curl_socks_tester.sh: line 1: kill: (-21325) - no such process
(redirect) alternatively running command , killing script using ctrl+c:
./curl_socks_tester.sh <(cut -f1 -d' ' data/socks_tested_sorted.list)
the script stops , following message:
^cterminated
why trap able kill child-processes when using redirect fails using pipe?
the way bash runs jobs, first link in pipe-line becomes group leader (as should able verify ps
).
consequently, in:
cut -f1 -d' ' data/socks_tested_sorted.list | ./curl_socks_tester.sh
./curl_socks_tester.sh
not group leader , kill -- -$$
invalid.
you can use kill -- -$$
in script if can sure shell script leader of process group.
Comments
Post a Comment