Last active
June 20, 2018 08:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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