LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2010, 03:38 PM   #1
EnderX
Member
 
Registered: Nov 2006
Posts: 66

Rep: Reputation: 15
Unexpected results: Difference in Perl Script Behavior between manual and cron runs


If this is in the wrong forum, I apologize. It deals with perl programming, but the behavior change exists, as noted, between a manual shell run and a cron-driven run; if that means this belongs in another forum, I respectfully request that my question be moved where it belongs.


Everything below is running on SuSE machines; either 9.3 or 11.3. I've had the same behavior on each.

I'm trying to set up an email system to warn me when something fails on the servers I'm requested to maintain. I've got a perl script that I had to write some time back that can email files for me, so I scavenged from it for this purpose.

I'm writing all of the error streams into a directory '/home/log_mail/' on each server. A copy of my script is supposed to run hourly, and send any output .txt files in that directory to me, then rename them so they won't be sent a second time. (Renaming from '$name'.txt to '$name'.txt.old)

This works. However, since I'm getting output files from stderr on the projects I'm mapping to this, I'm getting a number of blank files in this directory. In order to keep the script(s) from spamming me, I'd thought to toss in a filesize marker to determine whether or not a file actually had something in it; if the size is zero, the script will not mail the file.

I know about the '-s' option; I'm trying to use that in my code for the size-based filter I wanted. When I've run the script manually, I've gotten the test files in that directory that I knew should go. However, while running as a scheduled task (called from cron.hourly or in the crontab file), I'm not getting anything.

I created a dummy project that did the same split, only without any of the email-calling routines. I'm getting the same output results from it as from the original: it works properly on manual run, but not on cron-driven run.

Below is the code of my dummy script:
Code:
#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;
use Net::SMTP;
use MIME::Lite;

my $count;
my $p1;

my $datadir = "/home/log_mail/";
my $filesz;

opendir DH, "$datadir" or die " could not read listing: $!";
while ($_ = readdir(DH))
{
        if (m/.txt/g)
        {
                $p1=pos $_;
                $count++;
                print "\n$count" . ') ';
                print $_, " " x (30-length($_));
                print "\n";
                my $filesz = -s "$_";
                if (0 == $filesz)
                {
                        print "File $_ had zero file size.\n";
                }
                else
                {
                        print "File $_ had file size $filesz\n";
                }
        }
}
Also,the contents of the directory I'm working with:
Code:
total 16
drwxrwxrwx   2 root root 208 Dec  9 15:30 .
drwxr-xr-x  14 root root 440 Dec  9 15:15 ..
-rwxr-xr-x   1 root root 615 Dec  9 15:13 email-Dummy
-rw-r--r--   1 root root 773 Dec  9 15:31 email-log.err
-rw-r--r--   1 root root 145 Dec  9 15:31 email-log.log
-rw-r--r--   1 root root   0 Dec  9 14:36 kumquat.txt
-rw-r--r--   1 root root  47 Dec  9 14:58 rutabega.txt
When I run this manually, I get the following results:
Code:
1) rutabega.txt
File rutabega.txt had file size 47

2) kumquat.txt
File kumquat.txt had zero file size.
When I let the crontab run it, with output to the .log file on stdout and the .err file on stderr, I get the following:
Code:
Log File:

1) rutabega.txt
File rutabega.txt had zero file size.

2) kumquat.txt
File kumquat.txt had zero file size.


Err File:
Use of uninitialized value in numeric eq (==) at /home/log_mail/email-Dummy
        line 26 (#1)
    (W uninitialized) An undefined value was used as if it were already
    defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
    To suppress this warning assign a defined value to your variables.

    To help you figure out what was undefined, perl tells you what operation
    you used the undefined value in.  Note, however, that perl optimizes your
    program and the operation displayed in the warning may not necessarily
    appear literally in your program.  For example, "that $foo" is
    usually optimized into "that " . $foo, and the warning will refer to
    the concatenation (.) operator, even though there is no . in your
    program.
With that information, it seems fairly clear that the program is blowing up on the size comparison for rutabega.txt; it's treating it as undefined on a cron run. (It may be doing the same for kumquat.txt as well; I can't tell based on this.) I don't understand why it would behave that way, though, given that it works when running the script manually. What am I missing here that's causing the difference?

Last edited by EnderX; 12-09-2010 at 03:42 PM. Reason: Forgot OS version.
 
Old 12-09-2010, 04:03 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Try checking what 'my $filesz = -s "$_";' returns before you use it, also you don't need the 'my' as you declared it earlier
 
Old 12-10-2010, 07:29 AM   #3
EnderX
Member
 
Registered: Nov 2006
Posts: 66

Original Poster
Rep: Reputation: 15
I apologize, I'm not quite certain I understand what you are saying. From what I've read, I was under the impression that the perl -s option would show the filesize in bytes of a file passed to it, and this does appear to be the behavior exhibited in a manual run. In what manner am I mistaken in this?
 
Old 12-10-2010, 11:42 PM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Quote:
Use of uninitialized value in numeric eq (==) at /home/log_mail/email-Dummy
line 26 (#1)
This indicates that '$filesz' is not being initialised so you need to check it's value before you attempt the comparison operation
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script runs if executed from CL, not from Cron Ravendark Programming 6 10-13-2010 06:07 AM
Script runs manually but not from cron redvelo Linux - Newbie 6 09-29-2010 01:37 PM
python rsync script runs from shell not from cron beanbox Programming 3 11-18-2009 06:23 PM
bash script runs different when cron executed jalder85 Linux - Server 4 02-20-2009 11:53 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration