Created
September 29, 2023 15:53
-
-
Save tsjk/3f05a70d2f403d6b062561cee0bae37c to your computer and use it in GitHub Desktop.
Functions for getting process lists and process trees of podman containers from the host's perspective
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pdmn-ps() { | |
# With this one can do like, given a container called tor-client, | |
# kill -HUP $(pdmn-ps tor-client | \ | |
# grep -F ' tor -f /config/torrc --defaults-torrc /config/torrc.custom.default' | \ | |
# awk '{ print $2 }') | |
# Useful for when one has both a client and a relay, and has a smaller overhead than exec-ing into the container. | |
local container_pid | |
[[ -n "${1}" ]] && podman container exists "${1}" && { \ | |
container_pid=$(podman inspect --format '{{.State.Pid}}' "${1}") | |
ps -o 'user,pid,%cpu,%mem,vsz,rss,tty,stat,lstart,time,command' \ | |
$(ps -e --no-header -o pid,ppid | awk -vp=${container_pid} 'function r(s) { print s; s=a[s]; while(s) { sub(",", "", s); t=s; sub(",.*", "", t); sub("[0-9]+", "", s); r(t); }; } { a[$2]=a[$2]","$1; } END { r(p) }') | |
} | |
} | |
function pdmn-pstree() { | |
local container_pid | |
[[ -n "${1}" ]] && podman container exists "${1}" && { \ | |
container_pid=$(podman inspect --format '{{.State.Pid}}' "${1}") | |
pstree -TUaclp -H ${container_pid} ${container_pid} | |
} | |
} | |
_pdmn_ps_completions() { | |
if [ "${#COMP_WORDS[@]}" != "2" ]; then | |
return | |
fi | |
local suggestions=($(compgen -W "$(podman ps --format '{{.Names}}' | sort)" -- "${COMP_WORDS[1]}")) | |
if [ "${#suggestions[@]}" == "1" ]; then | |
local number="${suggestions[0]/%\ */}" | |
COMPREPLY=("$number") | |
else | |
for i in "${!suggestions[@]}"; do | |
suggestions[$i]="$(printf '%*s' "-$COLUMNS" "${suggestions[$i]}")" | |
done | |
COMPREPLY=("${suggestions[@]}") | |
fi | |
} | |
complete -F _pdmn_ps_completions pdmn-ps | |
complete -F _pdmn_ps_completions pdmn-pstree |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment