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

When I run my script on commandline it works fine, but through cron it displays error:

mkdir //.ssh: Permission denied at /usr/lib/perl5/vendor_perl/5.8.5/Net/SSH/Perl/Util/Hosts.pm line 92

I am using module Net::SSH::Perl in my perl script

use Net::SSH::Perl; $pac = burn.i386_26.rpm $host = "10.x.x.x"; $user = "xxxxx"; $passwd = "xxxxx"; $cmd = <<COM; cd /usr rpm -ivh $pac COM $ssh = Net::SSH::Perl->new($host); $ssh->login($user,$passwd); ($output, $error, $exit) = $ssh->cmd($cmd); $cmd = <<CC; cd /usr rm -rf $pac CC $ssh = Net::SSH::Perl->new($host); $ssh->login($user,$passwd); ($output, $error, $exit) = $ssh->cmd($cmd);

Please assist, thanks!!

Replies are listed 'Best First'.
Re: Script running through cron throwing error else works fine
by aaron_baugher (Curate) on Apr 25, 2012 at 00:35 UTC

    Generally, when something works at the command line but not in cron, it's because of a difference in the environment. Two things to check:

    1. Is the cron task running as the same user as when you run it at the command line? In other words, is the cron owned by you, and are you running it as yourself on the command line? Or if you're running it as root, is the crontab owned by root?

    2. What else is different in the environment? For instance, is there are difference in the $PATH environment variable? Is there a difference in the current working directory? (My guess from your error message is that your cron is setting your $HOME or pwd to the root (/) directory.) Add debugging lines to your script to output these things, and see how they compare to when you're running it manually. For instance, run this script at the command line and in cron, and see what changes:

    #!/usr/bin/perl print "user: ", `whoami`; print "pwd: ", `pwd`; print "environment variables:\n"; print "$_: $ENV{$_}\n" for sort keys %ENV;

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

      Thanks Aaron, I tried your suggestion, yes cron uses root(/), for NET::SSH::PERL and I am running this script on my username cron, that's the reason it's failing. You were correct. Could you please suggest under my user cron how can I run this script

Re: Script running through cron throwing error else works fine
by rovf (Priest) on Apr 25, 2012 at 08:47 UTC
    mkdir //.ssh: Permission denied at /usr/lib/perl5/vendor_perl/5.8.5/Net/SSH/Perl/Util/Hosts.pm line 92
    (1) Did you have a look at line 92?

    (2) If we believe the error message, it tries to create a directory //.ssh. The two slashes are strange in this context. Could it be, that the directory to be created, should be something like $ENV{HOME}/.ssh, and HOME happens to be set to '/'?

    -- 
    Ronald Fischer <ynnor@mm.st>