#!/usr/bin/env bash
max=20;
while :
do
mem=$(ps ax -o pid=,%mem= | sort -rnk2 | head -n1)
cpu=$(ps ax -o pid=,%cpu= | sort -rnk2 | head -n1)
perl -le 'exit(1) if $ARGV[0] > $ARGV[1]' ${cpu## [0-9]* } $max ||
echo "Max cpu exceeded. Cpu: $cpu" | mail faheem@mitha.com && exit;
perl -le 'exit(1) if $ARGV[0] > $ARGV[1]' ${mem## [0-9]* } $max ||
echo "Max memory exceeded: $mem" | mail faheem@mitha.com && exit;
sleep 1;
done