Friday, September 02, 2005

capture important information about processes

This simple ad hoc script could help to capture important information
about processes, but could also be easily modified to do additional
things -- such as start the process if it isn't running or log the
number of such processes that are running at each check interval. The
complete script appears below for easy cutting and pasting.

<pre>
#!/bin/bash # watchProc: monitor a process and log start and stop times

sleepInterval=60
logDir=/var/tmp

# ---- is proc name supplied? ----
if [ "$1" = "" ]; then echo -n "watch?> " read procName else procName=$1 fi

# ---- determine if process(es) are running at invocation ----
prevStatus=`pgrep $procName | wc -l | awk '{print $1}'` dt=`date`

# ---- if proc not currently running, ask user to confirm monitoring
---- if [ "$prevStatus" = 0 ]; then yn=`ckyorn -p "$procName is not
running now -- monitor anyway?" -d y` case $yn in [Yy]*) echo "$dt:
starting monitor task for $procName" | tee -a $logDir/$procName.log
echo "$dt: $procName is down" | tee -a $logDir/$procName.log ;; [Nn]*)
echo ok exit ;;

* echo exiting exit ;; esac else echo "$dt: starting monitor task
for $procName" | tee -a $logDir/$procName.log echo "$dt: $procName is
running" | tee -a $logDir/$procName.log fi

# ---- continue monitoring at configured interval ---- while true do
dt=`date` newStatus=`pgrep $procName | wc -l | awk '{print $1}'` if [
$newStatus != $prevStatus ]; then case $newStatus in 0) echo "$dt:
$procName is down" >> $logDir/$procName.log ;;

* echo "$dt: $procName is running" >> $logDir/$procName.log ;;
esac prevStatus=$newStatus fi sleep $sleepInterval done </pre>

0 Comments:

Post a Comment

<< Home