Skip to content

Instantly share code, notes, and snippets.

@ryul99
Created July 11, 2019 07:21
Show Gist options
  • Save ryul99/f2f1e2d7fdcf1b5918e51f2f4d2cc536 to your computer and use it in GitHub Desktop.
Save ryul99/f2f1e2d7fdcf1b5918e51f2f4d2cc536 to your computer and use it in GitHub Desktop.
get pids of descendants processes of docker container and itself
#!/bin/bash
# get docker container self and descendants pids
docker_container_pids=()
docker_container_descendants=()
for container_id in $(docker ps --format '{{.ID}}'); do
a=$(docker inspect -f '{{.State.Pid}}' "$container_id")
docker_container_pids+=( "$a" )
done
for pid in ${docker_container_pids[@]}; do
for descendant in $(pstree -p $pid | grep -o '([0-9]\+)' | grep -o '[0-9]\+'); do
docker_container_descendants+=( "$descendant" )
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment