#!/bin/sh PS=/bin/ps SLEEP=/bin/sleep KILL=/bin/kill LOGGER=/usr/bin/logger TAG=rss-watchdog usage() { echo "$0 -p -k [-w ] [-s ]" echo " Defaults: -w 5 -s SIGTERM" exit 0 } secs=5 signame=SIGTERM while getopts p:k:w:s:? o; do case $o in p) pid=$OPTARG;; k) kb=$OPTARG;; w) secs=$OPTARG;; s) signame=$OPTARG;; ?) usage;; esac done if [ -z "$pid" -o -z "$kb" ]; then usage fi $LOGGER -t $TAG "Starting (pid=$pid rss=$kb wait=$secs sig=$signame)" while true; do rss=$($PS -p $pid -o rss=) if [ -z "$rss" ]; then $LOGGER -t $TAG "Exiting (pid=%pid vanished)" break fi if [ $rss -gt $kb ]; then $LOGGER -t $TAG "Killing (pid=$pid rss=$rss>$kb sig=$signame)" $KILL -s $signame $pid fi $SLEEP $secs done