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.

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