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.


In reply to Re: Script running through cron throwing error else works fine by aaron_baugher
in thread Script running through cron throwing error else works fine by pinnacle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.