[rc5] lw: nicer than nice unix shell script process monitor

Frederick W. Wheeler wheeler at ipl.rpi.edu
Fri Oct 10 09:45:37 EDT 1997


I use this script to control my bovine clients.  It only allows them
to run after normal working hours, and then only if there is no other
significant machine load.  

--- begin lw --------------------------------------------------------------
#! /bin/sh

# lw - load watcher version 1.1
#
# Usage: lw process_id
# 
# Starts and stops another process to keep it in an extremely nice
# batch mode.  The machine load, time of day, and day of week are
# monitored by lw.  The batch process is allowed to run only outside
# of normal working hours.  If there is any other signifigant machine
# usage, the batch process is stopped.  When the machine has no
# signifigant usage, the batch process is restarted.  The thresholds
# used to determine machine usage from the load averages are defined
# at the beginning of the script and are easy to modify.
# 
# The 1, 5 and 15 minute average machine loads are found using
# uptime(1) and a long sed command.  The load is converted from a
# decimal to an integer by removing the decimal point.  This is done
# because test(1) ([] in sh if statements) can only compare integers.
# A load average of 1.23 is represented as 123.
# 
# The machine load and time are checked by lw every minute.
# 
# Fred Wheeler (wheeler at ipl.rpi.edu)
# Oct 6, 1997

# start if all load averages are below these values, in hundredths
ld_run1=20
ld_run5=20
ld_run15=20
# stop if any load average is above these values, in hundredths
ld_stop1=140
ld_stop5=120
ld_stop15=120

# how much output to print, 0: none, 1: starts/stops, 2: each check
verbose=1

# get the pid of the batch process from the command line
batch_pid=$1

# exit with error status if signaled
trap "exit 1" 1 2 15

# print a message on exit
trap 'echo lw: killed `date`' 0

# initial state of the batch process is assumed to be run
batch_state=r

# infinite loop
while true
do

  # extract three 3 digit integers representing the load
  cmd=`uptime | sed \
    -e 's/.*load average:\(.*\)/\1/' \
    -e 's/\.//g' \
    -e 's/,//g' \
    -e 's/ *\([0-9]*\) *\([0-9]*\) *\([0-9]*\)/ld1=\1;ld5=\2;ld15=\3;/'`
  eval $cmd

  # get the hour of the day and the day of the week
  hour=`date +%H`
  day=`date +%w`

  # default new batch process state is the old batch process state
  new_batch_state=$batch_state

  # decide if batch process state should be changed
  if [ $batch_state = r ]; then
    if [ $ld1 -ge $ld_stop1 -o $ld5 -ge $ld_stop5 -o $ld15 -ge $ld_stop15 \
       -o \( $day -ge 1 -a $day -le 5 -a $hour -ge 7 -a $hour -le 17 \) ]; then
      new_batch_state=s
    fi
  elif [ $batch_state = s ]; then
    if [ $ld1 -le $ld_run1 -a $ld5 -le $ld_run5 -a $ld15 -le $ld_run15 \
       -a \( $day -lt 1 -o $day -gt 5 -o $hour -lt 7 -o $hour -gt 17 \) ];then
      new_batch_state=r
    fi
  else
    echo lw: error, unknown batch process state name
    exit 1
  fi

  # find signal name
  if [ $new_batch_state = r ]; then
    signal=CONT
  elif [ $new_batch_state = s ]; then
    signal=STOP
  else
    echo lw: error, unknown batch process new state name
    exit 1
  fi

  # determine if state has changed
  if [ $batch_state != $new_batch_state ]; then
    state_change=1
  else
    state_change=0
  fi

  # send signal to batch process
  kill -$signal $batch_pid
  kill_status=$?

  # error message and exit if batch process could not be signaled
  if [ $kill_status -ne 0 ]; then
    if [ $verbose -ge 1 ]; then
      echo lw: exiting, could not signal batch pid $batch_pid, `date`
    fi
    exit 1
  fi

  # display state transition, load and time is verbose set high enough
  if [ \( $state_change = 1 -a $verbose -ge 1 \) -o $verbose -ge 2 ]; then
    sc=${batch_state}-${new_batch_state}
    echo lw: sc:$sc pid:$batch_pid ld:$ld1 $ld5 $ld15 hr:$hour dy:$day `date`
  fi

  # update batch process state
  batch_state=$new_batch_state

  # sleep for one minute
  sleep 60

done
--- end lw ----------------------------------------------------------------
----
To unsubscribe, send email to majordomo at llamas.net with 'unsubscribe rc5' in the body.



More information about the rc5 mailing list