Skip to content

Instantly share code, notes, and snippets.

@RecNes
Last active June 20, 2018 08:40
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 RecNes/211bdeef7b8eab823866 to your computer and use it in GitHub Desktop.
Save RecNes/211bdeef7b8eab823866 to your computer and use it in GitHub Desktop.
This script will displays or sends email for RAM usage statistics of python scripts
#!/usr/bin/env sh
#title : ram_usage.sh
#description : Shell script for displaying RAM and Thread statistics of python scripts.
#author : Sencer HAMARAT "sencerhamarat(at)gmail.com"
#date : 20161014
#version : 0.4
#usage : sh ram_usage.sh
#==============================================================================
#!/usr/bin/env bash
SERVERNAME=$(hostname -f)
SUBJECT=" Python scripts RAM usage statistics for ${SERVERNAME} host"
printf "$SUBJECT\n"
printf "%-62s %13s %5s %8s %15s\n" "PROCESS" "RAM USAGE" "PID" "STATUS" "Thread Counts"
printf "=============================================================================================================\n"
ps ax --no-headers -o pid,vsz,stat,command | awk -v lim=400000 '
# let awk do the grepping
/python/ && !/awk/ {
# save first 3 fields
pid=$1
vsz=$2/1024
stat=$3
tthreads_cmd="cat /proc/"pid"/status|grep Threads";
tthreads_cmd | getline tthreads;
close(tthreads_cmd)
# rest is command line, possibly spanning multiple fields
for (i=4;i<=NF;++i) $(i-3)=$i
NF-=3
# decide on color
if (vsz>lim) col="\033[31m"
else if (vsz>lim/2) col="\033[33m"
else col="\033[32m"
# printout
printf("%s%-62s %10d KB %5s %6s %s%s\n", col, $0, vsz, pid, stat, tthreads, "\033[0m")
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment