rajyalakshmi has asked for the wisdom of the Perl Monks concerning the following question:

Everyday a perl script is run by crontab .The script runs a query and dumps results in the file results.txt and hence everyday results.txt changes . I have another script that sends mail with 'results.txt' as attachement.

The problem is when the script is run manually,the results.txt changes .But when the script is run through cronjob,the results.txt does not change.However the mail sent has updated or changed results.txt.But when i see results.txt in the server i do not see changes.BUT CHANGES ARE seen in the mail..

here is my script to dump results in file:

#!usr/bin/perl use DBI; use DBD::mysql; use POSIX; $scripttime=localtime(time); @frequency=("4","10","24","48","168","240"); $user="root"; $password="bnhgfbjf"; $database="kgjthiohjo"; $dsn = "dbi:mysql:$database:localhost:3306"; $dbh= DBI->connect($dsn, $user, $password) or die "Couldn't connect to database: "; open (FILE, ">results.txt" )or die "Can't open results output file: $! +"; print FILE "**************start scripting '$scripttime'*************** +***\n"; foreach $frequency(@frequency) { $timestamp=strftime("%Y-%m-%d %H:%M:%S",localtime(time-3600*$frequency +)); $query="xyz"; print "$timestamp"; print "\n"; print "$frequency"; print "\n"; my $sth = $dbh->prepare($query); my $rv = $sth->execute or die "($DBI::err): $DBI::errstr"; print FILE "***********Fetch not started for the following categories +of frequency'$frequency' hours********************\n"; $sth->dump_results( 80, "\n", "-> ", \*FILE ); sleep(5); } close FILE or die "Error closing result file: $!\n";

Replies are listed 'Best First'.
Re: problem in dumping sql results in a file
by Corion (Patriarch) on Jul 21, 2010 at 11:48 UTC
    open (FILE, ">results.txt" ) or die ...

    The current directory is not what you think it is. And as you run that cron job as root, and root is allowed to write everywhere, you don't get an error, but a file results.txt somewhere on your filesystem. Maybe it is in /results.txt.

    chdir to the proper location before writing your file or use absolute path names in the output name of the file.

Re: problem in dumping sql results in a file
by scorpio17 (Canon) on Jul 21, 2010 at 18:44 UTC
    Another suggestion: don't use two scripts. Write one script that generates the attachment AND ALSO emails it. Even better, generate the attachment in memory, and don't even bother writing it to a file.
Re: problem in dumping sql results in a file
by talexb (Chancellor) on Jul 21, 2010 at 16:46 UTC

    And as a suggestion, Trying updating the script so that the filename contains a time and date stamp (results-2010-0721-1245.txt, for example), then making a sym-link from the current version of the file to 'results.txt'.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds