Skip to content

Instantly share code, notes, and snippets.

@serious-angel
Created January 9, 2026 17:46
Show Gist options
  • Select an option

  • Save serious-angel/3a742d7b563c535801231971b77584c2 to your computer and use it in GitHub Desktop.

Select an option

Save serious-angel/3a742d7b563c535801231971b77584c2 to your computer and use it in GitHub Desktop.
Basic Process Tree with Teletype (Bash script)
#! /usr/bin/env bash
declare c=0;
_PidTree()
{
declare __pid="$1";
shift || return 2;
declare stat=(); readarray -td ' ' stat < "/proc/${__pid}/stat";
declare cmd=(); readarray -td '' cmd < "/proc/${__pid}/cmdline";
declare ty; ty="$( readlink -e "/proc/${__pid}/fd/0"; )";
declare ppid="${stat[3]}";
printf " [%s] [%6s | %-6s] TY=%-10s '%s' [%s]\n" "$(( c++ ))" "$__pid" "$ppid" "$ty" "${cmd[0]}" "${cmd[*]:1}";
if [[ $__pid != 1 ]];
then
_PidTree "$ppid";
fi
}
_Main()
{
declare __pid="${1:-$$}";
shift || return 2;
printf '%s\n' ' PID PPID';
_PidTree "$__pid" | sort -rk 1,1;
}
_Main "$@";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment