Skip to content

Instantly share code, notes, and snippets.

@DJStompZone
Created July 24, 2024 13:52
Show Gist options
  • Save DJStompZone/e310a7782514f20d0fdcca3b8fdb5996 to your computer and use it in GitHub Desktop.
Save DJStompZone/e310a7782514f20d0fdcca3b8fdb5996 to your computer and use it in GitHub Desktop.
PSJ: Display Running Processes in JSON Format

PSJ

Gnarly little "one liner" in awk and bash. Searches for running processes and displays the PID and command line for any matches, in JSON format.

Screenshot_20240724-064347_Termux

Usage:

psj [process name or keyword] [--running] [--alias]
  • --running: Shows only running processes

  • --alias: Shows the original caller processes (By default, shows the actual process names, not the parents)

  • [process name or keyword]: A filter string, will show only matching processes

#!/bin/bash
psj() { local filter ps_args="-A " add_c=true; while [[ $# -gt 0 ]]; do case "$1" in --running) ps_args+="r"; shift ;; --alias) add_c=false; shift ;; *) filter="$1"; shift ;; esac; done; $add_c && ps_args+="c"; ps_args+="o "; ps --no-headers $ps_args 'pid,cmd' | grep "$filter" | awk 'BEGIN {print "{"} {pids[NR] = $1; cmds[NR] = $2} END {for (i = 1; i <= NR; i++) { printf " \"%s\": \"%s\"", pids[i], cmds[i]; if (i < NR) printf ","; print "" } print "}"}'; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment