#!/bin/sh
#---------#---------#---------#---------#---------#---------#---------#---------
#
# title
#   check the news files and restart the process if nothing new arrived
# author
#   idc@planetlarg.net 13 August 01
# description
#   The  news company have provided us with a client that regularly
#   collects news files from their servers.
#   It locks up during system upsets like server restarts.
#   As it does no logging and does not crash, the only way to check that it is
#   still working is to monitor the ages of the files it downloads.
#   If these are more than 12 hours old, restart the server.
#   No proper restart script has been provided, so kill the process and run
#   a shell script that fires it up as a background nohup process.
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------
# 1. check the download directories
# if no file has been added within the last 12 hours, a restart is needed.
    # the current time in unix format (seconds since 1970)
    current=`perl -e 'print time;'`
    # the last modified time of the most recent file in unix seconds
    file=`./dir-last-modified.pl /batch/SiCloneSubscriber/OLG*`
    # age difference in seconds
    diff=`expr $current - $file`
    # maximum length of time
    # seconds x minutes x hours
    max=`expr 60 \* 60 \* 12`

    restart=0
    if [ $diff -gt $max ]; then
        restart=1
    fi

# 2. report on what we find
    echo "$current      $file"

# 3. if files are too old, restart the server
    if [ $restart -eq 0 ]; then
        exit
    fi

    # kill the original
    pid=`ps -ef | grep SiClone | egrep -v grep |  cut -c10-15`
    kill $pid

    # did kill work?
    if [ $? -gt 0 ]; then
        echo "MyTrap: stale news files and SiClone client refuses to die"
        exit
    fi
    # start a fresh one
    cd /batch/SiCloneSubscriber/
    ./Pull
    sleep 10

    # did the restart work?
    new_pid=`ps -ef | grep SiClone | egrep -v grep |  cut -c10-15`
    if [ $new_pid -lt 1 ]; then
        echo "MyTrap: stale news files and SiClone client refuses to restart"
        exit
    fi


