Forum Moderators: coopster & phranque

Message Too Old, No Replies

perl and cron job to update site content by server time.

         

mstevenson

3:42 pm on May 19, 2006 (gmt 0)

10+ Year Member



I am looking for a way to automate our special process on our website (have to manually uload special html pages and images at the beginning and end of each month, including the count down each day towards the end).

I origianlly thought JavaScript was the way to go to do this (see my original thread at [webmasterworld.com...] but the more I think about it, I think a perl script and cron job would be even better.

Basically, I have to update several .html pages that have the special information on them (index.html and some service pages) and also upload new special graphics.

I think it would be ideal if I could create a folder in the www folder, probably named the date I want to move the files out of that folder into the main www folder (i.e. name the folder 051906/) and then create a cron job that checks the server every night at 12:00 to see if there is a folder that matches that days date, and if so, it moves the contents of the folder to the main www folder.

I only have a working knowledge of perl and cron, I can not create my own, but I can usually fiddle with other peoples scripts and get them to work. Is what I described above possible (and practicle)? Any help is appreciated.

KevinADC

11:41 pm on May 19, 2006 (gmt 0)

10+ Year Member



there are many tutorials on the web for how to setup cron jobs so google for cron tutorials.

As far as the perl side goes, look into the rename() function or using the File::Copy module which has an actual move() function I believe, although rename() really does the same thing. The operating system can probably handle this too, but that depends on the OS, perl will be a better cross platform solution if that is a consideration.

lexipixel

2:23 am on May 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If there is any chance you want to archive the data and you are going to name folders by date, I would use /YYYYMMDD/ ie-

20060101 (Jan 1, 2006)
20060515 (May 15, 2006)
20060330 (Mar 30, 2006)
etc..

The reason being that they will sort (if archiving and ever finding the data again is is a concern).

Here's some chunks of code that take care of a few Perl date handling issues, (adding 1900 to year, left padding single digit days, figuring days in month, leap year, etc..) They are coded in a verbose manner so you can actually read them.

#
#
($dd,$mm,$yyyy) = (localtime)[3,4,5];
$yyyy = ($yyyy + 1900);
if($dd < 10){ $dd = "0" . $dd; }
if($mm < 10){ $mm = "0" . $mm; }
#
# here's today's date as YYYYMMDD
#
$YYYYMMDD = "$yyyy$mm$dd";
#
#

And here's a sub that will find the number of days in a month...

#
# uncomment next line to debug dates and code
# $debug = "YES";
#

#================
sub DaysInMonth {
#================
#
my $y = $_[0];
my $m = $_[1];
#
if ($debug eq "YES") { print "<BR><B>sub DaysInMonth</B><BR>\n"; }
if ($debug eq "YES") { print "y = $y<BR>\n"; }
if ($debug eq "YES") { print "m = $m<BR>\n"; }
#
if (substr("$m",0,1) eq '0') { $m = substr("$m",1,1); }
#
my $days = '31'; # (default / max value)
#
if ($m eq '1') {
if (isLeapYear($y) eq '1' ) {
if ($debug eq "YES") { print "y = $y<BR>\n"; }
if ($debug eq "YES") { print "isLeapYear(y) = " . isLeapYear($y) . "<BR>\n"; }
$days = '29';
} else {
$days ='28';
}
}
if (($m eq '3') ¦¦ ($m eq '5') ¦¦ ($m eq '8') ¦¦ ($m eq '10')) { $days = '30'; }
#
if ($debug eq "YES") { print "days = $days<BR>\n"; }
#
return $days;
#
}
#
#

You'll also need to know if it's a leap year for February...

#===============
sub isLeapYear {
#===============
#
my $year = $_[0];
#
if ($debug eq "YES") { print "<BR><B>sub isLeapYear</B><BR>\n"; }
if ($debug eq "YES") { print "year = $year<BR>\n"; }
#
if ((($year % 4 == 0) && ($year % 100!= 0)) ¦¦ ($year % 400) == 0) {
return '1';
}
return '0';
}
#
#

Now just duct take them together with your file generating / copying code and you've got a script...<grin>..

Or you could just use one of the CPAN date modules --- (I prefer to roll my own so I don't have to depend on modules being installed).

mstevenson

7:46 pm on May 24, 2006 (gmt 0)

10+ Year Member



Wow, thanks for your help, that helps alot. I really look forward to making this a more automated process, and your comments will definately help me do this. Thanks for the code.