bash. while loop with xargs kill -9 -
i have list of ip addresses , have run command every single ip address.
i did code:
array=($(</tmp/ip-addresses.txt)) in "${array[@]}"; ./command start $i & done
now, list of ip addresses refreshed every 2 minutes , need kill every command no longer new ip addresses. practically, command needs executed again every 2 minutes refreshed ip addresses , old ip needs killed.
how can that?
a simple workaround: (not tested)
sleep_delay=120 # 2 mins while true; ( array=($(</tmp/ip-addresses.txt)) in "${array[@]}"; ./command start $i & done sleep $(( sleep_delay + 2 )) # 2 can number >0 ) & ppid=$! sleep $sleep_delay pkill -9 -p $ppid done
note: have not optimized code, added wrapper around code.
edit: edited code satisfy requirement old processes should not killed, if ip still same.
note: haven't tested code myself, careful while using kill command. can test putting echo
before kill
statement. if works well, can use script...
declare -a pid_array while true; array=($(</tmp/ip-addresses.txt)) in `printf "%s\n" ${!pid_array[@]} | grep -v -f <(printf "%s\n" ${array[@]})`; kill -9 ${pid_array[$i]} # please try use better signal kill, sigkill unset pid_array[$i] done in "${array[@]}"; if [ -z "${pid_array[$i]}" ]; ./command start $i & pid_array[$i]=$! fi done sleep 120 done
Comments
Post a Comment