Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

perl script and cron

by snyder (Initiate)
on Mar 23, 2006 at 18:44 UTC ( #538821=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, I am having a problem running a perl script via cron.. Linux ES4 v2. I run the script at command line and it runs fine. I run it through cron and it does not seem to wait until the tarball is fully created before it FTP's the incomplete tarball. is there a way to make the script complete the "tar" before begining the "ftp"?
#!/usr/bin/perl use IO::Dir; tie %dir, 'IO::Dir', "club"; foreach (keys %dir) { next if $_ eq "."; next if $_ eq ".."; push( @files, "club/" . $_ ); } use Archive::Tar; my $tar = Archive::Tar->new; $tar->add_files( @files ); $tar->write('club_db.tar'); use Net::FTP; $ftpobj = Net::FTP -> new ("192.168.1.21"); $ftpobj -> login("username","passwd"); $ftpobj -> binary; $ftpobj -> put ("club_db.tar"); $ftpobj -> quit;

Replies are listed 'Best First'.
Re: perl script and cron
by derby (Abbot) on Mar 23, 2006 at 20:15 UTC

    snyder ... this is just like your last post (tar help). I'm just guessing that your IO::Dir is failing since you use a relative path here instead of the absolute path you used before. Before you create the tar file, add some debug statements to see if you actually have any data in your @files array.

    -derby
      well, looks like adding the full path to the directory worked. After some thought and derby's post, I concluded the script, running as a cron job is no longer running from within the parent directory therefore the full path is requireed. Thanks derby.
      derby, with all due respect the post is not the same. I now have a working script. For some reason when cron runs it (as root) it FTP's a tarball to the remote server with the proper name. The problem is the the tar did not complete before the FTP portion of the script was executed. The file that is FTP'd is 1k and should be 60megs. If I run it from command line, it runs fine. The tarball is 60 megs and it show up on the remote server as such. The path to the directory is not an issue as I am running the script from the parent directory.
        The path to the directory is not an issue as I am running the script from the parent directory.

        Yes, from the console but not from cron. When you run a program with cron you should safely assume that the current working directory is pretty much arbitrary. Therefore you need to either use an absolute path when checking the directory or explicitly set the current directory using chdir.

Re: perl script and cron
by QM (Parson) on Mar 23, 2006 at 20:05 UTC
    What makes you think it starts the FTP before the tarball is finished?

    If you're judging by received file size, is it not more likely that the FTP hangs up in the middle?

    Unless Archive::Tar makes use of threads or forks off a process, then it seems unlikely that FTP starts before tar finishes.

    One other possibility is that tar->write is filling the disk, fails, and then the FTP starts.

    We need more info to help you out.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      the question is, why does it only fail when cron runs it? If i run it from consol, it runs like a champ everytime.
Re: perl script and cron
by Argel (Prior) on Mar 24, 2006 at 01:39 UTC
    Scripts running under cron usually run in a very shall we say reduced envrionemnt. Relying on envrionment variables (including PATH), current directories, etc. will usually get you in trouble. You are much better off being as explicit as possible (e.g. use the full path to all commands).
Re: perl script and cron
by socketdave (Curate) on Mar 23, 2006 at 20:07 UTC
    I'm not too familiar with Archive::Tar, but this sounds like it could be a permission or path problem. When run by cron, does the tarball get created where you expect it? If so, does it get to the same size every time? Are you sure that you are running the script as the same user from the command line and from cron?
      the cron script runs as root, I run the script form consol as SU (root) and the file size is the same everytime (6 tries). When cron runs it it is 1k and exactly where it is supposed to be localy and remotely. When I run from consol it does exactly as it should. tarball is 60 megs and where is should be localy and remotely.
Re: perl script and cron
by mercutio_viz (Scribe) on Mar 23, 2006 at 21:30 UTC

    Snyder,

    I was curious if you could try something. Could you add a sleep(60) right before the use Net::FTP? I'm just curious to see what would happen. If the FTP is truly starting before the $tar is finishing, then this might yield a different result than the 1K file you've been getting. It isn't a fix, but possibly it might yield some more information.

    Let us know what happens!

    -MC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://538821]
Approved by idsfa
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2020-12-01 00:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?