The little things give you away... A collection of various small helper stuff
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

53 řádky
1.8 KiB

  1. #!/bin/bash
  2. set -e
  3. if [[ "$1" == '-h' || $# -ne 2 ]]
  4. then
  5. echo 'Usage: kill-wpull-connections (-p PID | -j JOBID | -h)'
  6. echo
  7. echo ' -h: Display this message and exit'
  8. echo ' -j JOBID: Kill connections of the wpull process for ArchiveBot job JOBID'
  9. echo ' -p PID: Kill connections of wpull process PID'
  10. [[ "$1" == '-h' && $# -eq 1 ]] && exit 0 || exit 1
  11. fi
  12. if [[ "$1" == '-p' ]]
  13. then
  14. wpullPid=$2
  15. if [[ "${wpullPid}" == *[^0-9]* ]]
  16. then
  17. echo "Error: '${wpullPid}' is not a valid PID"
  18. exit 1
  19. fi
  20. elif [[ "$1" == '-j' ]]
  21. then
  22. pids=($(pgrep --full "wpull.*$2"))
  23. # Filter out this script; this is more complicated than it should be because you can't simply `| grep -v` the PID of this script. When you do that, you create a subshell, which you'd need to filter out as well.
  24. # Instead, we convert the list to a string of the form " 1 2 3 ", then replace " PID " by a single space, and finally convert it back to an array for counting the remaining PIDs.
  25. pids=" ${pids//$'\n'/ } "
  26. pids=(${pids/ $$ / })
  27. if [[ ${#pids[@]} -ne 1 ]]
  28. then
  29. echo "Error: not exactly one process found for '$2'"
  30. exit 1
  31. fi
  32. wpullPid=${pids[0]}
  33. fi
  34. if ! kill -0 ${wpullPid} >/dev/null 2>&1
  35. then
  36. echo "Error: no process with PID ${wpullPid}"
  37. exit 1
  38. fi
  39. gdb -batch -batch-silent \
  40. -ex "attach ${wpullPid}" \
  41. -ex 'shell echo "FDs before forced shutdown:"; lsof -an -p '${wpullPid}' -i TCP | grep -v 127\.0\.0\.1' \
  42. -ex 'python import subprocess' \
  43. -ex 'python for fd in subprocess.check_output("lsof -an -p '${wpullPid}' -i TCP -F pfn | awk '\''NR%2==0{fd=substr($0,2)}NR%2==1&&NR>1&&!/127\.0\.0\.1/{print fd}'\''", shell = True).decode("ascii").strip().split("\n"): gdb.execute("p shutdown(" + fd + ", 2)")' \
  44. -ex 'shell echo "FDs after forced shutdown:"; lsof -an -p '${wpullPid}' -i TCP | grep -v 127\.0\.0\.1' \
  45. -ex detach \
  46. -ex quit