http://www.perlmonks.org?node_id=1032807

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

Hi Monks, Here is the riddle! I have a script that runs fine if executed on command prompt. What script does is copy files from FTP, decompresses it, parse and prints out certain test data I am interested in. However if the script is run through cron, I see correct output in the cron notification but I don't see the actual files generated in the directory. Seems like some compatibility issue. Below is the o/p from the script (on command prompt)
Please be patient, files are being copied over from FTP site Unzipping apr25_03_45.stdf.gz.. Converting apr25_03_45.stdf to txt Done!
Below is the o/p from the script (via Cron)
Please be patient, files are being copied over from FTP site Unzipping apr25_03_45.stdf.gz.. gunzip: apr25_03_45.stdf already exists; not overwritten Converting apr25_03_45.stdf to txt Converting apr27_10_12.stdf to txt Done!

Replies are listed 'Best First'.
Re: Perl script runs through Cron but does not generate files
by blue_cowdawg (Monsignor) on May 09, 2013 at 18:07 UTC

    Here is a piece of code for you:

    #!/usr/bin/perl -w use strict; my $mode=shift @ARGV; my $logfile=sprintf("/tmp/%s-env.txt",$mode); open FOUT,"> $logfile" or die "$logfile: $!"; foreach my $key ( sort keys %ENV){ printf FOUT "%s = \"%s\"\n",$key,$ENV{$key}; } close FOUT; system(sprintf "umask >> %s",$logfile); system(sprintf "id >> %s",$logfile); system(sprintf "pwd >> %s",$logfile); exit(0);
    Don't forget to  $ chmod 755 /path/to/my/script/above.pl so it can run. First run it as a "batch" job
    $ echo "/path/to/my/script/above.pl ascron" | at now
    which will be the same environment as a cronjob would be. Then run it from the command line:
    $ /path/to/my/script/above.pl ascmdlin
    then check the differences:
    diff /tmp/ascron-env.txt /tmp/ascmdlin-env.txt
    and that should help you rule some stuff out.

    Typically the difference between command line and cron has to do with permissions or environment being different. It ain't rocket science.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Perl script runs through Cron but does not generate files
by hdb (Monsignor) on May 09, 2013 at 17:58 UTC

    This might be an environment issue. Commands under cron are not having the same environment compared to a user logging in. Have you taken care of all path and variables that could influence your script?

Re: Perl script runs through Cron but does not generate files
by Anonymous Monk on May 09, 2013 at 19:02 UTC
    Guesses include user-id running under cron does not have access. Check system logs.
Re: Perl script runs through Cron but does not generate files
by hotpelmen (Scribe) on May 09, 2013 at 20:33 UTC
    Print out current working directory and you will see where you are unzipping your files:
    use Cwd (); print "Current directory: " . Cwd::cwd . "\n";
      There are many possible points of failure. Brief list of things to check:

      1. in your script, do you chdir to the directory where you need the files before you download them? If that's missing, you need to chdir first.

      2. if you chdir before ftp but the files are not there, then printing current working directory as suggested above would be helpful.

      3. make sure you handle errors properly. Use absolute paths for your log files where you log the errors. Check all your operations for errors, check file sizes, etc.