Hi all, I have a script which updates a DBI::CSV file
at 12 midday every day. When I run the program from
the command line, all works as expected, but when run
by cron, nothing happens (even when it should). Now my
entry in the crontab file is :
0 12 * * * /path/to/script
After run by cron, DBI->trace level two shows:
DBI 1.14-nothread dispatch trace level set to 2
<- FETCH= HASH(0x1e5948)0keys ('csv_tables' from cache) at cryodynes l
+ine 23
.
-- DBI::END
-> disconnect_all in DBD::File::dr for DBD::CSV::dr (DBI::dr=HASH(
+0x1958a0)~
0x23c714)
<- disconnect_all= undef at DBI.pm line 450.
-> DESTROY in DBD::File::db for DBD::CSV::db (DBI::db=HASH(0x23c70
+8)~INNER)
<- DESTROY= undef during global destruction.
-> DESTROY in DBD::File::dr for DBD::CSV::dr (DBI::dr=HASH(0x23c71
+4)~INNER)
<- DESTROY= undef during global destruction.
The code of 'script' is below. Any ideas as to why the
script doesn't run via cron would be appreciated.
Thanks, Stacy.
#!/usr/local/bin/perl -w
use DBI;
chdir('/path/to/script') || die "Can't chdir: $!\n";
$rxdb = 'rxdb.dat';
$updb = 'uptime_db';
$recdb = 'receiver_db';
$trace = 'trace.log';
unlink("$trace") if (-e "$trace");
#Connect to receiver_db (CSV format)
$dbh = DBI->connect("DBI:CSV:f_dir=/p/smader/cryo");
$dbh->{'RaiseError'} = 1;
DBI->trace(2,$trace);
$dbh->{'csv_tables'}->{'receiver_db'} =
{
'eol' => "\n",
'col_names' => ["INSTALLED", "SERIAL", "UPTIME", "PACKAGE"],
};
#Get current time in Epoch seconds
$now_time = time;
#Get current modify time of rxdb.dat
$current_mtime = (stat("$rxdb"))[9]; #In Epoch seconds
#Get previous modify time
$previous_mtime = qx(cat $updb); #In Epoch seconds
chomp($previous_mtime);
die "$updb contains non-digit value!\n"
if ($previous_mtime !~ m/^\d+/);
if ($current_mtime eq $previous_mtime) {
#rxdb.dat hasn't been modified in the last
#24 hours. Add 24 to values in $recdb.
&update(24);
exit;
} elsif ($previous_mtime < $current_mtime) {
#rxdb.dat has been modified. Get number of
#hours since last modification and update
#$recdb. Put current mtime into $updb.
#Backup receiver_db
$td_str = &format_date(1);
system "cp -f $recdb $recdb.$td_str";
#Get number of hours since rxdb.dat was changed.
$hours = ($current_mtime - $previous_mtime) / 3600;
&update($hours);
#Reset $updb to current mtime of rxdb.dat
open(TMP,">$updb") or die "Can't open $updb: $!\n";
print TMP "$current_mtime";
close(TMP);
exit;
} else {
die "$rxdb modification time greater than current time!\n";
exit;
}
######################################################
sub update {
($add) = @_;
#See which receivers are present AND available
open(DB, "$rxdb") or die "Can't open $rxdb: $!";
$i = 0;
while (<DB>) {
$i++;
if (m/^(\$\$package)/) {
#Is package present & available?
@db = `more $rxdb`;
if ($db[$i+14] =~ m/yes/ && $db[$i+15] =~ m/yes/) {
$rx_name = (split '=', $db[$i])[1];
$rx_name =~ s/^\s+//; #remove leading whitespace
$rx_name =~ s/\s+$//; #remove trailing whitespace
if ($rx_name =~ m/^[57]0CM/) { $test = '50/70CM'; }
elsif ($rx_name =~ m/^[SCX]\-BAND/) { $test = 'ATPROTO'; }
elsif ($rx_name =~ m/^GALILEO/) { $test = 'GALILEO'; }
elsif ($rx_name =~ m/^K/) { $test = 'K/Ku'; }
elsif ($rx_name =~ m/^METH/) { $test = 'METHANOL'; }
elsif ($rx_name =~ m/^MULTI/) { $test = 'MULTI'; }
elsif ($rx_name =~ m/^(H-OH)/) { $test = 'H-OH'; }
#Get previous uptimes from $recdb, and add to them.
open(TMP,"$recdb") or die "Can't open $recdb: $!\n";
while (<TMP>) {
chomp;
($uptime,$package) = (split(',',$_))[2,3];
if ($package eq $test) {
$new_uptime = $uptime + $add;
}
}
close(TMP);
$dbh->do(q{UPDATE receiver_db SET UPTIME = ? WHERE PACKAGE = ?
+},
'undef',$new_uptime,$test);
}
}
}
close(DB);
return;
}
######################################################
sub format_date {
my ($input) = @_;
($min,$hour,$mday,$month,$year) = (localtime($now_time))[1..5];
$min = sprintf "%02d", $min;
$hour = sprintf "%02d", $hour;
$mday = sprintf "%02d", $mday;
$month = sprintf "%02d", $month+1;
$year += 1900;
$time = join('',$hour,$min);
$date = join('',$mday,$month,substr($year,-2,2));
$td_str = join('_',$time,$date);
if ($input eq '0') {
return($min,$hour,$mday,$month,$year);
} else {
return($td_str);
}
}