Cron Job & Perl

G

goooo

I've got a perl script that I'm having problems trying to launch from CRON.

My script deletes UPDATE.INI, checks to see if file DAT version specified in
UPDATE.INI is same in directory. (ie. DAT-4450.ZIP) If not it deletes old
file and downloads the latest file.

It works perfect when I launch it manually from cmd prompt. But via CRON.
All it seems to do is delete the UPDATE.INI & DAT files and bombs out.

Heres my cron line -

24 11 * * * /usr/bin/perl
/etc/periodic/naidat_update.pl >/dev/null 2>&1

Heres my script -
#################################################################
#!/usr/bin/perl
use Net::FTP;
use Cwd;
my $dir = "/home/dats";
my $olddat = "unknown";
my $newdat = "unknown";

opendir (DIR, $dir) or die $!;
my @dir = readdir DIR;
closedir DIR;

foreach (@dir) {
if(/dat\-/){
$olddat = substr($_,4,4);}
}

print "Old DAT file Version is ","$olddat","\n\n";
system("rm","-rf","$dir/update.ini");

print "Downloading latest UPDATE.INI file\n\n";

system("wget","ftp://ftp.nai.com/pub/antivirus/datfiles/4.x/update.ini","-P"
,"/home/dats");

open(FILE, "$dir/update.ini");

while (<FILE>){
chomp;
if (/FileName\=/ && /dat\-/ && /\.zip/i){
$newdat = substr($_,13,4);}
}
close FILE;

print "New DAT file Version is $newdat\n\n";

if($olddat!=$newdat){
print "Deleting old DAT file $olddat\n\n";
system("rm","-rf","$dir/dat-$olddat\.zip");
print "Downloading latest DAT file $newdat\n\n";

system("wget","ftp://ftp.nai.com/pub/antivirus/datfiles/4.x/dat-$newdat\.zip
","-P","/home/dats");

} else{

print "DAT file Up To Date\n\n";}
 
T

Tad McClellan

goooo said:
It works perfect when I launch it manually from cmd prompt. But via CRON.


Must be something different in the environment then, cwd, env vars, etc...

All it seems to do is delete the UPDATE.INI & DAT files and bombs out.


What does "bombs out" mean when you say it?

Get any messages or anything like that?

#################################################################
#!/usr/bin/perl

use warnings;
use strict;

opendir (DIR, $dir) or die $!;


What directory are you in here?

foreach (@dir) {
if(/dat\-/){


hyphens are not special in regexes, there is no need to backslash them.

$olddat = substr($_,4,4);}
}


$olddat = $ 1 if /dat-(....)/;

print "Old DAT file Version is ","$olddat","\n\n";


Hnuh? What's with all the quotes and commas?

print "Old DAT file Version is $olddat\n\n";

open(FILE, "$dir/update.ini");


You should always, yes *always*, check the return value from open():

open(FILE, "$dir/update.ini") or
die "could not open '$dir/update.ini' $!";

if (/FileName\=/ && /dat\-/ && /\.zip/i){
^ ^
^ ^ more needless backslashing

if (/FileName=/ && /dat-/ && /\.zip/i){

if($olddat!=$newdat){


Whitespace is not a scarce resource, feel free to use as much as
you like to make your code easier to read and understand:

if ( $olddat != $newdat ){
system("rm","-rf","$dir/dat-$olddat\.zip");
^
^ needless backslashing yet again
 
J

Joe Smith

goooo said:
It works perfect when I launch it manually from cmd prompt. But via CRON.
All it seems to do is delete the UPDATE.INI & DAT files and bombs out.

system("wget","ftp://ftp.nai.com/pub/antivirus/datfiles/4.x/update.ini","-P"
,"/home/dats");

If your "wget" is someplace other than /bin or /usr/bin (such as
/usr/local/bin), then it's no wonder that it cannot be found
under cron.

Use absolute pathname, or set $ENV{PATH} to a known value.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top