Operating System - HP-UX
1748246 Members
3421 Online
108760 Solutions
New Discussion юеВ

Help on file time stamp monitoring

 
SOLVED
Go to solution
RamkumarA
Respected Contributor

Help on file time stamp monitoring

Hi Experts,

I have a file in which i need to monitor whether the file is updating in timely manner. Can someone help me in designing a script to send an alert (using mailx) if the file is not updated for last 5 minutes?

Thanks,
Ramkumar A.
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: Help on file time stamp monitoring

Hi:

You could do something like this:

# cat ./monitor
#!/usr/bin/perl
my $secs = shift or die;
my $file = shift or die;
while (1) {
die "Missing file '$file'\n" unless -f $file;
if ( ((stat($file))[9]) < (time() - $secs) ) {
system qq( mailx -s "File Alert" < /dev/null yourname@\@your.com > /dev/null );
}
sleep(5); #...adjust as desired...
}
1;

...run as:

# ./monitor seconds filename

...as to monitor a file for update every 600 seconds:

# ./monitor 600 /path_to_file

Regards!

...JRF...
Michael Steele_2
Honored Contributor

Re: Help on file time stamp monitoring

hi

access time / atime
ls -u

Use time of last access

modification time / mtime
ls -c

Use time of last modification of the inode (file created, mode changed, etc.) for sorting (-t) or printing (-l (ell)).

initialization -or- create time / ctime
ls -c

Also see 'stat' command

# stat filename

time_t st_atime; /* Time of last access */
time_t st_mtime; /* Last modification time */
time_t st_ctime; /* Last file status change time */

http://docs.hp.com/en/B2355-90693/stat.2.html
Support Fatherhood - Stop Family Law
RamkumarA
Respected Contributor

Re: Help on file time stamp monitoring

Hi James,

Thanks for the script. I ran the script against the old file(/tmp/ramtest1.txt) like below and its not returning to command prompt and i didnt get the email too.

shcscp19# ./monitor 600 /tmp/ramtest1.txt

Please advise.

Thanks,
Ramkumar A.
James R. Ferguson
Acclaimed Contributor

Re: Help on file time stamp monitoring

Hi (again):

The line:

# system qq( mailx -s "File Alert" < /dev/null yourname@\@your.com > /dev/null );

...should have been:

# system qq( mailx -s "File Alert" < /dev/null yourname\@your.com > /dev/null );

Change the string "yourname\@your.com" to the address you want.

Regards!

...JRF...
RamkumarA
Respected Contributor

Re: Help on file time stamp monitoring

Hi James,

It was already present like you specified and my email address was updated. But still after running the script, its not returning to command prompt and no emails triggered.

shcscp19# cat monitor
#!/usr/bin/perl
my $secs = shift or die;
my $file = shift or die;
while (1) {
die "Missing file '$file'\n" unless -f $file;
if ( ((stat($file))[9]) < (time() - $secs) ) {
system qq( mailx -s "File Alert" < /dev/null ranantharam3@csc.com > /dev/null );
}
sleep(5);
}
1;
shcscp19# ./monitor 600 /tmp/ramtest1.txt

Please advise.

Thanks,
Ramkumar A.
James R. Ferguson
Acclaimed Contributor

Re: Help on file time stamp monitoring

Hi (again):

> But still after running the script, its not returning to command prompt and no emails triggered.

As I wrote the script, it runs until interrupted (with a Control_C). If the file represented by the second argument passed is older than the number of seconds specified by the first argument, then an email alert is generated. The assumption is the the file is constantly being updated.

If you want to script to test and exit, change it to:

# cat ./monitor
#!/usr/bin/perl
my $secs = shift or die;
my $file = shift or die;
die "Missing file '$file'\n" unless -f $file;
if ( ((stat($file))[9]) < (time() - $secs) ) {
system qq( mailx -s "File Alert" < /dev/null jim-ferguson\@smh.com > /dev/null );
}
1;

...run as before. To check is the file hasn't been updated in 5-minutes (600 seconds):

# ./monitor 600 file

This queries the 'mtime' of the 'stat()' structure which has a value in Epoch seconds.

Regards!

...JRF...