Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto checking a file for growth

Status
Not open for further replies.

icu812

MIS
Sep 10, 2001
52
US
Good morning Ladies and Gentlemen:
I have an error log that it is normal a zero byte file. When it grows to any length I want to be notified. How can I get a korn shell script to automate this? Does anyone know? Thank You
 
icu812,

Crudely, you could do.....

#!/bin/ksh

size=`ls -l /<path to file> |awk '{print $5}'`

if [ ${size} != &quot;0&quot; ];
then
echo &quot;size is not zero&quot;
else
echo &quot;size is zero&quot;
fi

Run this from the cron every 5 mins or whatever and replace the echo &quot;size is not zero&quot; with the command you wish to run

Hope that helps

PSD
IBM Certified Specialist - AIX V4.3 Systems Support
IBM Certified Specialist - AIX V4 HACMP
 
The -s option is &quot;file has a size greater than zero&quot;.

Put in your script to page you if return code is anything other than 1.

[ -s filename ];print $?
Your return code $? should be 1 if it is still at zero byte size. Your return code $? will be 0 if it has grown.


if [ -s filename ]
then
mail -s &quot;filename has grown&quot; you@company.com < filename
else
exit

This will include the contents of filename in your email text. Remember, amateurs built the Ark, professionals built the Titanic.
 
If you wanted something a bit more instant, then you could knock up a script ( say, myscript ) which reads from stdinput, and mails you a message as soon as it reads a line of input.

Then all you'd need to do is run a background task similar to the following:

tail -f logfilename | myscript

Something else to consider
Mark
 
The find command could be used as well. As in:

find {PATH} -name {FILE} -size +0 -exec {NOTIFY} \;

which I believe will only search the PATH for the FILE and will only run the NOTIFY command when the size is greater than zero. You could put that in a cron script described by PSD above. The benefits that find has over ls | awk are only a single command instead of two, and it would use less resources (I may be wrong - but calling awk for a simple one line check is wasteful).

Anyhow, I hope you find your answer.
Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top