!/bin/csh -f 
#Simple script to monitor size of process $1 for $2 seconds
# written by Matt Probert, appended by Luke Abraham 2005/11/10


set logfile=${PWD}/$1.size
set counts=0
set usage="usage: check_size pid {max_seconds}"

#check for PID
if ( $#argv == 0 ) then
   echo 'PID not set'
   echo $usage
   exit 1
endif

# warn if syntax error
if ( $#argv > 2 ) then
   echo 'too many arguments'
   echo $usage
   exit 2
endif

#... and if there is a second then it is #seconds
if ( $#argv == 2 ) then
   set max_counts = $2
else
#default to 1 hour
   set max_counts = 3600
endif

#check numeric value of max_counts
if ($max_counts < 1) then
   echo 'max_seconds < 1'
   echo $usage
   exit 3
endif


#DO IT
rm -f $logfile
while ($counts < $max_counts)
    set out_string=`ps -eo pid,rss,sz,vsz,%mem | grep $1 | awk -v secs=$counts '{printf "%10i %10i %10i %10i %10.3f\n",secs,$2,$3,$4,$5}'`
    if ( $#out_string == 0 ) then
	echo 'PID no longer exists'
        exit 4
    endif 
    echo $out_string >> $logfile
    sleep 1
    @ counts++
end