[ back to toc ]

cron job issues

Date: 2002/04/03 10:18

Q:
Hi there...i was creating a monthly cron job for thr tripwire logs and for
some reason, the date variable doesn't seem to work right. To ge tthe
previous month i need to use an equation, thus for jan - sept, i need to
reinsert a 0 to the front of the date. well i do just that....but when
the job is actually ran...it still just looks for the file without the
0....below is the actual script. Any thoughts on how to make this easier
or just to get to work would be greatly appreciated.

#!/usr/bin/perl

# this used to zip up the old tripwire logs
# from the previous month.

#log directory
$logdir = "/security/tripwire/logs/";
# set the year & month for zippin'
$year = "20".'date +%y';
$currmonth = 'date +%m';
# pull the /n chars.
chomp($year);
chomp($currmonth);

if ($currmonth [2,3,4,5,6,7,8,9,10]) #less than ten, add the 0, for
Jan-Sept logs
{
$currmonth = $currmonth -1; #it's last month, so take away one
$currmonth = "0".$currmonth; #tack on a "0" at the front so the
files can be found
}
elsif ($currmonth == 1) #this one must pull the logs from december
{
$currmonth = $currmonth +11; #to make it register as 12 or dec
$year = $year -1; #to set the date to the previous year
}
else
{
$currmonth = $currmonth -1; #pull it back a month for oct-nov logs
}

# here's the destination file for the zip
$destfile = $logdir."log-".$year."-".$currmonth.".tgz";

# zip the month files

system "tar cvzf $destfile $logdir$year-$currmonth*";
system "rm -f $logdir$year-$currmonth*";

A:
1. You code uses ' and not ` (backtick) which may be a mailer conversion.

2. I do not know what the line

if ($currmonth [2,3,4,5,6,7,8,9,10])

is supposed to do. I would write it:

$currmonth --;
if( $currmonth )
{
$currmonth = '0' . $currmonth if $currmonth < 10;
}
else
{
$currmonth = '12';
$year --;
}

Regards,
Peter

[ back to toc ]