The little things give you away... A collection of various small helper stuff
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

63 líneas
1.9 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. if [[ ${#pids[@]} -ne 1 ]]
  24. then
  25. echo "Error: not exactly one process found for '$2'"
  26. exit 1
  27. fi
  28. wpullPid=${pids[0]}
  29. fi
  30. if ! command -v lsof >/dev/null 2>&1
  31. then
  32. echo "Error: could not find lsof"
  33. exit 1
  34. fi
  35. if ! command -v gdb >/dev/null 2>&1
  36. then
  37. echo "Error: could not find gdb"
  38. exit 1
  39. fi
  40. if ! ps -p ${wpullPid} >/dev/null 2>&1
  41. then
  42. echo "Error: no process with PID ${wpullPid}"
  43. exit 1
  44. fi
  45. if [[ -e /proc/sys/kernel/yama/ptrace_scope && "$(< /proc/sys/kernel/yama/ptrace_scope)" != "0" && $EUID -ne 0 ]]
  46. then
  47. echo "Warning: /proc/sys/kernel/yama/ptrace_scope is not zero. You likely need to run this script as root."
  48. fi
  49. gdb -batch -batch-silent \
  50. -ex "attach ${wpullPid}" \
  51. -ex 'python import subprocess' \
  52. -ex 'python def call(s): return subprocess.call(s, shell = True) == 0' \
  53. -ex 'python call("echo '\''FDs before forced shutdown:'\''") and call("lsof -an -p '${wpullPid}' -i TCP | grep -v 127\.0\.0\.1") and ([gdb.execute("p shutdown(" + fd + ", 2)") 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")] or True) and call("echo '\''FDs after forced shutdown:'\''") and call("lsof -an -p '${wpullPid}' -i TCP | grep -v 127\.0\.0\.1")' \
  54. -ex detach \
  55. -ex quit