You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.4 KiB

  1. #!/bin/bash
  2. list_descendants ()
  3. {
  4. local children=$(ps -o pid= --ppid "$1")
  5. for pid in $children
  6. do
  7. list_descendants "$pid"
  8. done
  9. [[ -n "$children" ]] && echo "$children"
  10. }
  11. shim_search="^docker-containerd-shim"
  12. count_shim_processes=$(pgrep -f $shim_search | wc -l)
  13. if [ ${count_shim_processes} -eq 0 ]; then
  14. shim_search="^containerd-shim"
  15. count_shim_processes=$(pgrep -f $shim_search | wc -l)
  16. fi
  17. if [ ${count_shim_processes} -gt 0 ]; then
  18. # Find all container pids from shims
  19. orphans=$(pgrep -P $(pgrep -d ',' -f $shim_search) |\
  20. # Filter out valid docker pids, leaving the orphans
  21. egrep -v $(docker ps -q | xargs docker inspect --format '{{.State.Pid}}' | awk '{printf "%s%s",sep,$1; sep="|"}'))
  22. if [[ -n "$orphans" ]]
  23. then
  24. # Get shim pids of orphans
  25. orphan_shim_pids=$(ps -o pid= $(ps -o ppid= $orphans))
  26. # Find all orphaned container PIDs
  27. orphan_container_pids=$(for pid in $orphan_shim_pids; do list_descendants $pid; done)
  28. # Recursively kill all child PIDs of orphan shims
  29. echo -e "Killing orphan container PIDs and descendants: \n$(ps -O ppid= $orphan_container_pids)"
  30. kill -9 $orphan_container_pids || true
  31. else
  32. echo "No orphaned containers found"
  33. fi
  34. else
  35. echo "The node doesn't have any shim processes."
  36. fi