#!/bin/ksh # # Display the top memory users or CPU users via ps. # # Andy Welter # www.the-welters.com # # BUGS: # This script doesn't sort CPU time properly for processes using # more than 24 hours of CPU time. Re-writting this in perl is the # most practical way to fix this bug since that will give me a lot # better string manipulation and pattern matching control. # USAGE="hogs [-mem|-cpu]" if [ $# -ge 1 ]; then OPT=$1 else OPT="-mem" fi case $OPT in -c*) # The long string of sed commands is used to normalize the elapsed CPU time field so # that the processes are sorted by time properly. echo "Top CPU users" echo "cputime vsize started pid user command" ps -ea -o time -o vsz -o stime -o pid -o user -o comm | \ tail +2 | \ sed 's/ *..:.. / ZZ:&/' | sed 's/ *ZZ: */ 00:/' | sed 's/^ */ /' | sed s'/00:0:/00:00:/' | sort -rn | head -20 ;; -m*) echo "Top Memory users" echo "vsize cputime started pid user command" ps -ea -o vsz -o time -o stime -o pid -o user -o comm | \ tail +2 | sort -rn | head -20 ;; *) echo "$USAGE" exit 1 esac