Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

detect timestamp change and then execute a script

Posted on 2001-06-11
8
173 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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 …
Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
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 …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

829 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