in reply to Script running through cron throwing error else works fine

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.

Replies are listed 'Best First'.
Re^2: Script running through cron throwing error else works fine
by pinnacle (Acolyte) on Apr 25, 2012 at 19:00 UTC

    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