Cron Job & Perl

Discussion in 'Perl Misc' started by goooo, Mar 21, 2005.

  1. goooo

    goooo Guest

    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";}
     
    goooo, Mar 21, 2005
    #1
    1. Advertising

  2. goooo <> wrote:

    > 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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 21, 2005
    #2
    1. Advertising

  3. goooo

    Joe Smith Guest

    goooo wrote:

    > 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
     
    Joe Smith, Mar 22, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Banks

    Equiv of a CRON job?

    Andrew Banks, Apr 8, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    15,297
    PeteJ
    Jul 30, 2012
  2. Nikola Skoric

    cron job times out

    Nikola Skoric, Nov 24, 2006, in forum: Python
    Replies:
    0
    Views:
    320
    Nikola Skoric
    Nov 24, 2006
  3. Bob Anastasia

    Cron Job through Perl

    Bob Anastasia, Jun 26, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    131
    Ted Zlatanov
    Jun 26, 2006
  4. Bill H

    Ftp via Perl cron job

    Bill H, Sep 26, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    209
    J. Gleixner
    Sep 28, 2006
  5. Bucker

    Runs Fine In Perl :: Not As Cron Job

    Bucker, Jan 15, 2007, in forum: Perl Misc
    Replies:
    6
    Views:
    197
    -berlin.de
    Jan 17, 2007
Loading...

Share This Page