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.

37 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. count_shim_processes=$(pgrep -f ^docker-containerd-shim | wc -l)
  12. live_restore=$(docker info --format {{.LiveRestoreEnabled}} 2>/dev/null)
  13. if [ ${count_shim_processes} -gt 0 ] && [ -n "${live_restore}" -a "${live_restore}" == "true" ]; then
  14. # Find all container pids from shims
  15. orphans=$(pgrep -P $(pgrep -d ',' -f ^docker-containerd-shim) |\
  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" ]]
  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 "Either live-restore is turned off or the node doesn't have any shim processes."
  32. fi