The little things give you away... A collection of various small helper stuff
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

62 satır
1.9 KiB

  1. #!/bin/bash
  2. if [[ "$1" == '-h' || $# -ne 2 ]]
  3. then
  4. echo 'Usage: kill-wpull-connections (-p PID | -j JOBID | -h)'
  5. echo
  6. echo ' -h: Display this message and exit'
  7. echo ' -j JOBID: Kill connections of the wpull process for ArchiveBot job JOBID'
  8. echo ' -p PID: Kill connections of wpull process PID'
  9. [[ "$1" == '-h' && $# -eq 1 ]] && exit 0 || exit 1
  10. fi
  11. if [[ "$1" == '-p' ]]
  12. then
  13. wpullPid=$2
  14. if [[ "${wpullPid}" == *[^0-9]* ]]
  15. then
  16. echo "Error: '${wpullPid}' is not a valid PID"
  17. exit 1
  18. fi
  19. elif [[ "$1" == '-j' ]]
  20. then
  21. pids=($(pgrep --full "wpull.*/$2/"))
  22. if [[ ${#pids[@]} -ne 1 ]]
  23. then
  24. echo "Error: not exactly one process found for '$2'"
  25. exit 1
  26. fi
  27. wpullPid=${pids[0]}
  28. fi
  29. if ! command -v lsof >/dev/null 2>&1
  30. then
  31. echo "Error: could not find lsof"
  32. exit 1
  33. fi
  34. if ! command -v gdb >/dev/null 2>&1
  35. then
  36. echo "Error: could not find gdb"
  37. exit 1
  38. fi
  39. if ! ps -p ${wpullPid} >/dev/null 2>&1
  40. then
  41. echo "Error: no process with PID ${wpullPid}"
  42. exit 1
  43. fi
  44. if [[ -e /proc/sys/kernel/yama/ptrace_scope && "$(< /proc/sys/kernel/yama/ptrace_scope)" != "0" && $EUID -ne 0 ]]
  45. then
  46. echo "Warning: /proc/sys/kernel/yama/ptrace_scope is not zero. You likely need to run this script as root."
  47. fi
  48. gdb -batch -batch-silent \
  49. -ex "attach ${wpullPid}" \
  50. -ex 'python import subprocess' \
  51. -ex 'python def call(s): return subprocess.call(s, shell = True) == 0' \
  52. -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")' \
  53. -ex detach \
  54. -ex quit