Solved

detect timestamp change and then execute a script

Posted on 2001-06-11
8
175 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
[X]
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
  • 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to dynamically set the form action using jQuery.

738 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