Browse Source

Handle processes with too many open connections

master
JustAnotherArchivist 2 years ago
parent
commit
3e0b70be6b
1 changed files with 14 additions and 7 deletions
  1. +14
    -7
      kill-connections

+ 14
- 7
kill-connections View File

@@ -24,23 +24,30 @@ echo "Open connections:" >&2
lsof -a -p ${pid} -i TCP -n >&2
echo >&2

v4sports="$(lsof -a -p ${pid} -i4 -i TCP -n -F nP0 | grep -Pao '\x00n\d{1,3}(\.\d{1,3}){3}:\K\d+' | tr '\n' ' ' | sed 's/ $//; s/ / -s /g')"
if [[ "${v4sports}" ]]
readarray -t v4sports < <(lsof -a -p ${pid} -i4 -i TCP -n -F nP0 | grep -Pao '\x00n\d{1,3}(\.\d{1,3}){3}:\K\d+' | sed 's,^,-s ,')
if [[ ${#v4sports[@]} -gt 0 ]]
then
echo "Killing IPv4 connections" >&2
#TODO tcp-closer only supports up to 128 sports at once; split it up if there are more.
#TODO This may also kill connections we want to keep. tcp-closer does not allow specifying the full (src, sport, dst, dport) tuple...
tcp-closer -4 -s ${v4sports}
for ((i=0; i<${#v4sports[@]}; i+=64))
do
tcp-closer -4 ${v4sports[@]:${i}:64}
done
echo >&2
fi
v6sports="$(lsof -a -p ${pid} -i6 -i TCP -n -F nP0 | grep -Pao '\x00n\[[^\]]+\]:\K\d+' | tr '\n' ' ' | sed 's/ $//; s/ / -s /g')"
if [[ "${v6sports}" ]]
readarray -t v6sports < <(lsof -a -p ${pid} -i6 -i TCP -n -F nP0 | grep -Pao '\x00n\[[^\]]+\]:\K\d+' | sed 's,^,-s ,')
if [[ ${#v6sports[@]} -gt 0 ]]
then
echo "Killing IPv6 connections" >&2
tcp-closer -6 -s ${v6sports}
for ((i=0; i<${#v6sports[@]}; i+=64))
do
tcp-closer -6 ${v6sports[@]:${i}:64}
done
echo >&2
fi

sleep 1

echo "Open connections:" >&2
lsof -a -p ${pid} -i TCP -n >&2



Loading…
Cancel
Save