Operating System - HP-UX
1753765 Members
6064 Online
108799 Solutions
New Discussion юеВ

Re: Need to Monitor File Writes

 
Steve Lowe (IL)
Occasional Contributor

Need to Monitor File Writes

We have an application that writes to a logfile in the /tmp directory. If the application stops functioning, this logfile no longer is written to.

Any suggestions as to how I can monitor this logfile? 'Are You Functioning' writes occur approximately 15 seconds from within the application. Over a 7 day period, the file will grow in size to approximately 200MB.

TIA.

Steve Lowe
Aurora University
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor

Re: Need to Monitor File Writes

Hi Steve:

Look at the manpages for 'touch' and 'sh-posix'.

One way is to create a reference point (file) with 'touch -r' that captures your file's last modification timestamp. Then compare what you last saw with the current value using something like 'myfile -nt reffile'. If the you file has been updated, then your application is running.

Your script can loop, sleeping for 30-seconds; wake-up; compare the file timestamps; update the reference file timestamp to be that of your monitored log if they differ; and return to sleep.

If n-number of loops find that the log timestamp has not changed, email yourself an alert.

Regards!

...JRF...


A. Clay Stephenson
Acclaimed Contributor

Re: Need to Monitor File Writes

You could do an ls -l filename every few minutes and compare the size to the last size. If that is constant then the log has stpped growing:

#!/usr/bin/sh

FILENAME=/aaa/bbb/myfile
typeset -i LAST_SIZE=-1
typeset DELAY=300 # 5 minutes
typeset -i SIZE=0

LAST_SIZE=$(ls -l ${FILENAME} | awk '{print $5}')
while [[ 1 -eq 1 ]] # loop forever
do
sleep ${DELAY}
SIZE=$(ls -l ${FILENAME} | awk '{print $5}')
if [[ ${SIZE} -eq ${LAST_SIZE} ]]
then
echo "File ${FILENAME} ain't growing." >&2
fi
LAST_SIZE=${SIZE}
done

A little checking needs to be done such as does the file exist? but this should get you started.

If it ain't broke, I can fix that.