Operating System - HP-UX
1828221 Members
2126 Online
109975 Solutions
New Discussion

Monitoring a UNIX directory with alerting

 
SOLVED
Go to solution
Jeff Daigle
Advisor

Monitoring a UNIX directory with alerting

I am looking for a UNIX script that we
can use to monitor a UNIX directory. I
want to use this script via crontab. I
want the script to look for a specific
file (abc.txt) in a specific directory
(/jeff/files). If this file is there,
do nothing. If this file is not there,
alert us (using email, page, other
alerting). Is this possible?
6 REPLIES 6
Bill McNAMARA_1
Honored Contributor

Re: Monitoring a UNIX directory with alerting

via a simple ksh script using the test
variable in an if statement.
if the return code is anything other than true
you would use mailx to mail a message.
I'm sure by the end of the day you'll have a hundred elegant ways to do it.

Later,
Bill
It works for me (tm)
Andreas Voss
Honored Contributor

Re: Monitoring a UNIX directory with alerting

Hi,

whithin a script you can check for the file with eg.:

if [ ! -f /jeff/files/abc.txt ]
then
mailx -s 'File not found'
fi

Regards
Laurent Paumier
Trusted Contributor

Re: Monitoring a UNIX directory with alerting

if [ ! -f /jeff/files/abc.txt ]
then
mailx -s "file not found" user@host fi
James R. Ferguson
Acclaimed Contributor
Solution

Re: Monitoring a UNIX directory with alerting

Hi Jeff:

You could cron the following one-liner:

# [ -f /tmp/myfile ] && echo "alert!" |mailx -s "file present!" root

This would alert 'root' if /tmp/myfile is present.

If you want the alert to be if absent, do:

# [ -f /tmp/myfile ] || echo "alert!" |mailx -s "file present!" root

...JRF...
Abel Berger
Regular Advisor

Re: Monitoring a UNIX directory with alerting

Hi Jeff,

Try this :cd /jeff/files

if [ $? -eq 0 ]
then
if [ -f abc.txt ]
then
mailx -s 'File found'
fi
fi
exit

I hope this help

Regards

Abel Berger

A. Clay Stephenson
Acclaimed Contributor

Re: Monitoring a UNIX directory with alerting

Yes Jeff, you can. I suppose you would like to know how:

Try something close to this:

#!/usr/bin/sh
PATH=/usr/bin:${PATH}
export PATH

TARGET=/xxx/yyy/myfile
MAILTO="me@mycompany.com"

STAT=0
if [ ! -r "${TARGET}" ]
then
echo "File ${TARGET} missing." | mail ${MAILTO}
STAT=$?
fi
exit ${STAT}

Most pagers also have a SMTP interface so by changing ${MAILTO} you can also send a page.

One way to do it, Clay




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