:
##########################################################################
# Shellscript:	checkps - check for old processes, notify user by mail
# Author     :	Heiner Steven (heiner.steven@odn.de)
# Date       :	07.02.1996
# $Id: checkps,v 1.2 2000/02/06 20:27:54 heiner Exp $
##########################################################################
# Description
#
##########################################################################

PN=`basename "$0"`			# Program name
VER=`echo '$Revision: 1.2 $' | cut -d' ' -f2`

Ignore="bin|daemon|news|root"		# Don't notify these users
Host=`uname -n`

Usage () {
    echo >&2 "$PN - check for old processes, $VER (stv '96)"
    exit 1
}

Msg () {
    for i
    do echo "$PN: $i" >&2
    done
}

Fatal () { Msg "$@"; exit 1; }

ForceRun=no
set -- `getopt fh "$@"` || exit 1
while [ $# -gt 0 ]
do
    case "$1" in
	-f)	ForceRun=yes;;
	--)	shift; break;;
	-h)	Usage;;
	-*)	Usage;;
	*)	break;;			# First file name
    esac
    shift
done

# This script may be run by cron - set path explicitely
PATH=$HOME/cmds:$PATH	export PATH

OldProcs="${TMPDIR:=/tmp}/cp$$"
trap 'rm -f "$OldProcs" >/dev/null 2>&1' 0
trap "exit 2" 1 2 3 13 15

if [ $ForceRun = no ]
then
    echo "Really check for old processes and notify users per mail (y/n)?" >&2
    read ok || exit 2
    case "$ok" in
	[nN]*)	exit 0;;
    esac
fi

# Get all processes at least one day old
psold | 
	egrep -v "^($Ignore)[ 	]" |
	egrep -v "<defunct>" > "$OldProcs" || exit 1

# Write mail to each user
for User in `nawk '++U [$1] == 1 {print $1}' "$OldProcs"`
do
    {	egrep "^$User[ 	]" < $OldProcs
	echo "[Diese Mail wurde automatisch erstellt]"
    } | mail -s "Alte Prozesse ($Host) - Absicht?" "$User"
done
exit 0
