Skip to content

Instantly share code, notes, and snippets.

@jahio
Created Mar 31, 2022
Embed
What would you like to do?
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