Solved

detect timestamp change and then execute a script

Posted on 2001-06-11
8
169 Views
Last Modified: 2013-12-25
I have a cron job(a perl program)  that I need to run only when the timestamp of a particular file changes.
Could anyone help with this?
Thanks.
0
Comment
Question by:chazhs
  • 5
  • 2
8 Comments
 
LVL 5

Expert Comment

by:Droby10
ID: 6188378
what part do you need help with...the timestamp valuation, the monitoring, the execution?
0
 

Author Comment

by:chazhs
ID: 6188443
The timestamp valuation.
I have developed the program that does the work I need. Only I need to execute it when a particular file a.txt changes. What I am guessing is that I should set a cronjob(that runs every minute may be)  to keep checking whether a.txt time stamp changed. If so, how do I track the change in  timestamp info of a.txt?

Thanks very much.
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6193274
you do it like this:

save this as watch.pl
and run it from cron

#!/usr/bin/perl
#watch.pl

$fileToWatch='funny.pl';

##################
#######this block of coke will make the file for you
#######in which we keep the record of the last modification
#######time in seconds since the epoch
#############
$file='time.dat';

if(-e $file)
{
 $lastTime=<FILE>;
 }
 else
 {
 open FIRSTTIME, ">$file";
 print FIRSTTIME ${[stat $fileToWatch]}[9];
 close FIRSTTIME;
 }

##################


open TIMER, $file;
$lastTime=<TIMER>;
close TIMER;
$time=${[stat 'funny.pl']}[9];

if($time>$lastTime){

###execute your action here
print "now is the time to take action";
##

open UPDATE, ">$file";
print UPDATE $time;
close UPDATE;
}
else{exit};


hope that helps,

Bob
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6193280
#######this block of coke will make the file for you

in this block of code...

there is nothing wrong with me, I promise.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 8

Accepted Solution

by:
bebonham earned 50 total points
ID: 6193283
wait, left over magic string...


$fileToWatch='funny.pl';

##################
#######this block of coke will make the file for you
#######in which we keep the record of the last modification
#######time in seconds since the epoch
#############
$file='time.dat';

if(-e $file)
{
$lastTime=<FILE>;
}
else
{
open FIRSTTIME, ">$file";
print FIRSTTIME ${[stat $fileToWatch]}[9];
close FIRSTTIME;
}

##################


open TIMER, $file;
$lastTime=<TIMER>;
close TIMER;
$time=${[stat $fileToWatch]}[9];

if($time>$lastTime){

###execute your action here
print "now is the time to take action";
##

open UPDATE, ">$file";
print UPDATE $time;
close UPDATE;
}
else{exit};



there ya go.  use this one please.
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6193299
how's married life treating you, Droby10?
0
 

Author Comment

by:chazhs
ID: 6193628
Bebonham, it works perfect!
Thanks much!!!!!
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6195125
glad to help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now