Solved

detect timestamp change and then execute a script

Posted on 2001-06-11
8
168 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
Comment Utility
what part do you need help with...the timestamp valuation, the monitoring, the execution?
0
 

Author Comment

by:chazhs
Comment Utility
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
Comment Utility
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
Comment Utility
#######this block of coke will make the file for you

in this block of code...

there is nothing wrong with me, I promise.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

Accepted Solution

by:
bebonham earned 50 total points
Comment Utility
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
Comment Utility
how's married life treating you, Droby10?
0
 

Author Comment

by:chazhs
Comment Utility
Bebonham, it works perfect!
Thanks much!!!!!
0
 
LVL 8

Expert Comment

by:bebonham
Comment Utility
glad to help!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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

11 Experts available now in Live!

Get 1:1 Help Now