in reply to Re^2: Zip script not running on cron
in thread Zip script not running on cron

Welcome to Perl and the Monastery, PerlMonger79!

My experience with cron has been that the safest thing to do is always use absolute pathnames for everything in the crontab and everything that is run from the crontab, which is basically also what Paladin meant with the CWD issue. In this case, I think it's unlikely that anything is affected by %ENV variables, but it's important to keep it in mind.

In this case, the first thing that jumps out at me is that you're not checking the chdir for errors, as in e.g. chdir "/path/to/directory" or die "chdir: $!";

Although not critical, there are several other suggestions I'd make:

Here's a script reworked with several of those suggestions:

#!/usr/bin/perl use warnings; use strict; use Time::Piece; use File::Spec::Functions qw/catfile/; use IO::Compress::Zip qw/zip $ZipError/; # Timestamp my $now = localtime; $now -= 60*60; $now = $now->strftime("%d%b%Y_%H00"); # Where zip file is to be stored my $arc = catfile("/path/to/archive","$now.zip"); die "Error: $arc already exists, refusing to overwrite" if -e $arc; # Where csv files are found. chdir "/path/to/directory" or die "chdir: $!"; # Zipping files found and storing into archive my @files = glob("*-$now.csv"); warn "Warning: No files to ZIP found\n" unless @files; zip \@files => $arc or die "Cannot create zip file: $ZipError" ; # Deleting csv files unlink(@files)==@files or warn "Failed to unlink some files: $!";

Update: A few grammar improvements. Apparently I work for the Department of Redundancy Department, apparently.

Replies are listed 'Best First'.
Re^4: Zip script not running on cron
by PerlMonger79 (Sexton) on Jul 18, 2019 at 20:50 UTC

    Thank you for the warm welcome and thank you very much for the suggestions and especially your version of the script. It worked perfectly ... I appreciate it very much. :)