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

I have recently installed the Net::SCP (ver 0.07) module within my server "Alpha". I then wrote a perl script which goes and puts files from server "Alpha" to "Beta". It works fine when run manually. Here's the script in it's entirety:
#!/usr/bin/perl use Net::SCP; $scp = Net::SCP->new ( "beta.kanja.gc.au", "guestUser"); $scp->cwd("/beta/path/router/files/") or die $scp->{errstr}; open(rtrList, "/alpha/path/list_of_files.lst"); while(<rtrList>) { chomp; $file = $_; $scp->put("$file") or die $scp->{errstr}; } close(rtrList);
Note: my $file scalar, in the code above, is the filename with its absolute path: /alpha/absolute/path/file1.cfg

Here's the problem...

When run as a cronjob, it fails once it gets within the while loop, specifically the last line of execution within loop: $scp->put. Yet I have nothing in my log file? Why? I then managed to find something in the unix mail addressed to me(root), which reads:
Your "cron" job on Alpha /alpha/script_path/fileXfr.pl > /tmp/fileXfr.log 2>$1 produced the following output: open3: exec of scp -pqB /alpha/absolute/path/file1.cfg guestUser@beta. +kanja.gc.au:/beta/path/router/files/file1.cfg failed at /usr/perl5/site_perl/5.6.1/Net/SCP.pm line 93
I have looked at line 93 in SCP.pm and I feel I'm in over my head at this point. Your advice would be much appreciated.

Thanks in advanced,

Papai

Replies are listed 'Best First'.
Re: scp fails in cronjob - open3 error
by pc88mxer (Vicar) on Jun 26, 2008 at 14:43 UTC
    When your script is being run by cron, it does not have it's PATH environment variable set up to include the scp command. One way to fix this is to add:
    $ENV{'PATH'} = "/location/of/scp:".$ENV{'PATH'};
    at the top of your script.
      I'd prefer to set cron's PATH instead. Also, it's a good practice to log the output of cronned tasks or set your mail address for the error output (so you won't have to wait for your root to forward you the message) The crontab would look like:
      MAILTO=me@me.com PATH=/path/to/scp 0 0 * * * /my/script
      I performed the following from my command prompt:
      aplpha:/# which scp /usr/local/bin/scp
      So I then added the line
      #!/usr/bin/perl $ENV{'PATH'} = "/usr/local/bin/scp:".$ENV{'PATH'};
      So the second line from the top, with the $ENV defined and the script still doesn't work? I get the same error.

      Is my path defined properly?

      Papai
        I got it working, my removed "scp" in my path and it now works.

        Cool, I have grown from this experience,

        Thanx.

        Papai