views:

173

answers:

4

So, OK, the job runs in cron: it fetches compressed files and processes them. If the files are corrupted, it deletes them. Sometimes they are bad on the remote server, in which case they will be downloaded and deleted each time.

My cron logs extensively to STDOUT (directed to logfile in .crontab), using STDERR only for things that cause the script to stop: I like getting emails from cron when bad stuff happens; it's just that corrupted files should not be on this list.

I need the output from 'gunzip' to tell me if the file is corrupted. However, I am tired of getting emails from cron every time it encounters a bad file. How do I call 'gunzip' so that errors will not trigger emails from cron, while still letting the script that calls 'gunzip' know that it failed?

This is probably a pretty easy one, but I'm pretty new to this cron stuff.

Important PS: 'gunzip' is called from a Perl script, using

$gunzip_result=system("gunzip $gzfile");
if($gunzip_result){
  print,"$gzfile is bad: deleting...\n"; 
  unlink $gzfile;
};
A: 

I've not tested it, but something like:

  $gunzip_result=system("gunzip $gzfile 2>/dev/null");
scragar
G B
Yay! An answer that doesn't involve yet another module!
Ed Hyer
+5  A: 

You might like to consider using IO::Uncompress::Gunzip which has better error control and handling. Here is a snippet that might help:

use IO::Uncompress::Gunzip qw(gunzip $GunzipError);

eval (
     gunzip $infh => $outfh or die $GunzipError
);
if $@ <do something with $GunzipError>

$GunzipError narrates what went wrong

Gurunandan
A: 

In your crontab, you can say

* * * * * foo.pl 2> /var/log/foo.pl.log

Cron won't see the errors any more, but you can still find them.

Chas. Owens
The question stipulates that getting mail from cron is a feature.
darch
A: 

You could use IPC::Run3:

run3(['gunzip', $gzfile], \undef, undef, \$stderr);
# close stdin, use existing stdout, redirect stderr
$gunzip_result = $? >> 8;
if($gunzip_result){
    print,"gunzip $gzfile is bad: ($stderr) deleting...\n"; 
    unlink $gzfile;
};
hillu