Skip to content

Instantly share code, notes, and snippets.

@jahio
Created March 31, 2022 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jahio/5eaacad1c23a00f96137fd13cf3a7b16 to your computer and use it in GitHub Desktop.
Save jahio/5eaacad1c23a00f96137fd13cf3a7b16 to your computer and use it in GitHub Desktop.
Gimme all processes using >= 200MB rss size on STDOUT in JSON format on Linux
#!/usr/bin/env bash
# Uses `ps` from $PATH (usually /usr/bin/ps) to get processes, then filters
# those by how much rss they're using; anything lower than the amount defined
# below as $THRESHOLD is discarded. Outputs JSON to STDOUT. No arguments.
declare -a pids
declare -a cmds
declare -a rss
# NOTE: This threshold is defined as KILOBYTES per `man ps`, not BYTES as in the
# PowerShell script. Adjust the math accordingly.
THRESHOLD=200000 # Don't report anything w/ RSS under this value (in kb)
while read data; do
this_rss=($(echo $data | awk '{print $2}'))
if [ "${this_rss}" -ge "${THRESHOLD}" ]; then
pids+=($(echo $data | awk '{print $1}'))
rss+=($this_rss)
cmds+=($(echo $data | awk '{print $3}'))
fi
done < <(ps -haxo pid,rss,command)
echo "{ \"processes\": ["
for i in "${!pids[@]}"
do
echo -n " {
\"pid\": \"${pids[$i]}\",
\"rss\": \"${rss[$i]}\",
\"command\": \"${cmds[$i]}\"
}"
if [ "${pids[$i]}" != "${pids[-1]}" ]
then
echo ","
fi
done
echo ""
echo "]}"
#
# Some notes:
# 1. This probably won't work without modification on MacOS (or anything not Linux)
# 2. Has not been tested beyond just my Linux box (Ubuntu 21.10)
# 3. This is only for quick-and-dirty demonstration purposes. It's not a real or supported tool.
# 4. Shell script wizards can probably do this a little better, maybe a lot better. Go for it! Just don't take pot shots
# because the fact is, you shouldn't *have* to be a wizard to be able to get useful things done.
# Gatekeeping is a sign of insecurity.
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment