Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Perl Module Not Working In Crontab

by graff (Chancellor)
on Jan 06, 2012 at 07:46 UTC ( #946546=note: print w/replies, xml ) Need Help??

in reply to Perl Module Not Working In Crontab

The two key suggestions above that are most important: (1) create a minimal script that focuses on the main problem (what differs between interactive use and cron-job use), and (2) use strict.

Apart from that, your script suffers from too much repetition of path information, ugly use of "." for string concatenation where a single double-quoted string will do, too many lines of code that could be removed by using a couple more core modules (e.g. POSIX, which provides the very handy "strftime()" function), and probably some errors that you aren't aware of.

For example, you have some lines like this:

$geoTiff_delete_all = </work/locust1/swarmsdata/RasterData/greening/*. +tif>; $zip_delete_all = </work/locust1/swarmsdata/RasterData/greening/*.zip> +;
Are you aware that if there are many *.tif and *.zip files, only one file name will be assigned to each of those variables? Is that your intention? Try this set of shell commands to see what I'm talking about:
cd /tmp touch perl -le '$f=<*.foo>; print "scalar: $f"; @f=<*.foo>; print "array: @f +"'

UPDATE: Just to give you an idea of how coding style relates to ease of coding and maintenance, the following snippet of 48 lines should be functionally the same as (or better than) the first 186 lines of the OP perl script; I'm using strictures, consistent indentation, simplest possible quoting, no repetition of literal strings, fewer variables, no redundant comments, and a "for" loop that eliminates several lines of repeated code:

#!/usr/bin/perl use strict; use warnings; use POSIX; use Archive::Extract; $Archive::Extract::PREFER_BIN=1; my $basedir = '/work/locust1/swarmsdata/RasterData'; my $greendir = "$basedir/greening"; my $dekadalGet = "$greendir/dekadalGet.txt"; my $dekadalNotGet = "$greendir/dekadalNotGet.txt"; my $user = 'xxxx'; my $pass = 'xxxx'; my $timeStamp = strftime( "%Y-%m-%d %H:%M:%S", localtime()); print "\n$timeStamp ****STARTING MODIS GREENING COLLECTION ROUTINE**** +\n\n"; for my $typ ( 'zip', 'tif' ) { print " -Checking dir for residual $typ file(s). \n"; my @delete_all = <$greendir/*.$typ>; if ( @delete_all ) { print " - $typ file(s) found purging directory. \n"; unlink @delete_all or die "$greendir/*.$typ - cannot be delete +d."; print " -Purge complete.\n\n"; } else { print " -No $typ file(s) found.\n"; } } print " -Checking dir for residual dekadalNotGet file.\n"; if ( -e $dekadalNotGet ) { print " -dekadalNotGet file(s) found purging directory.\n"; unlink $dekadalNotGet or die "$dekadalNotGet - cannot be deleted." +; print " -Purge complete.\n\n"; } else { print " -No $dekadalNotGet file found.\n"; } my ( $yr, $mo, $dy, $hr ) = ( $timeStamp =~ /(\d{4})-(\d\d)-(\d\d) (\d +\d):/ ); if ( $hr eq '06' and $dy =~ /[012]1/ ) { open( my $fh, '>', $dekadalGet ) or die "$dekadalGet: $!\n"; print $fh "$yr$mo$dy\n"; close $fh; print " -Collection day confirmed: added '$yr$mo$dy' to collection + file\n\n"; } print " **Starting dekadal collection procedure:**\n\n"; ...

I stopped at line 186 in your script ("Starting ... collection procedure") because after that point it was less clear to me what you were really trying to do (i.e. I'm not sure the OP code was actually doing what you intended). But I would expect you could get a similar (4-to-1) reduction for the rest of the script.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946546]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2018-06-21 10:26 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.