Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Perl module path not found

by Anonymous Monk
on Oct 07, 2015 at 13:18 UTC ( [id://1144061]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!

My code runs fine, I can even run it from the browser and it does what it is supposed do to, but I can’t get it to work running from a “cron job”, I tried everything, I had the "DBStuff.pm” locally, meaning in the same directory level as the “check.pl”, now I have it inside of "lib" directory, but it is still giving me this error:
Can't locate DBStuff.pm in @INC (@INC contains: .. /etc/perl /usr/loca +l/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/li +b/site_perl .) at /home/mysite.com/cgi-bin/scripts/check.pl line 10. BEGIN failed--compilation aborted at /home/mysite.com/cgi-bin/scripts/ +check.pl line 10.

The directory structure looks like this:
.. /cgi-bin /lib/ DBStuff.pm /scripts/ check.pl ..
Here is the part of the code:
#!/usr/bin/perl use strict; use warnings; use CGI qw(escapeHTML); use Data::Dumper; use Date::Calc qw( Today_and_Now ); use lib '..'; # Add private library to path use DBStuff; ####>>>>>>>>>>>>>> This is my line 10 use MyLib::Log qw(do_log); if ($@) { warn "Cron job failed: $@\n"; } my $q = new CGI; $| = 1; BEGIN { my $log_error = 'error.txt'; use CGI::Carp qw(carpout); # Send all warnings to the log_file open STDERR, '>>', $log_error; } ...
Any suggestions?
Thanks for looking!

Replies are listed 'Best First'.
Re: Perl module path not found
by stevieb (Canon) on Oct 07, 2015 at 13:25 UTC

    I suspect that it's because you are using a relative path to your lib directory.

    Try changing this:

    use lib '..';

    To this (of course, use the real full path):

    use lib '/path/to/cgi-bin/lib';
      use lib '/path/to/cgi-bin/lib';

      Should not
          use lib '../lib';
      also work? (I put this in the form of a question because I'm not sure on this point and have done no research or experimentation.)

      Update: Added  <blockquote> to reference specific path to which my question was directed.


      Give a man a fish:  <%-{-{-{-<

        In this case, it may not work. It all depends on who's crontab the script is executed from and what that user's home directory is. For instance, if it is in the Apache user's crontab and their home is /var/www, then that's where the script would be executed, and would not know how to find the 'lib' dir from there, as it would first have to locate 'cgi-bin'.

        One can test where cron scripts are being run out of by using the following code snip:

        #!/usr/bin/perl use warnings; use strict; use Cwd; print getcwd();

        Then, set up a crontab to call the script and output the result to a file (this entry is for root):

        $ sudo crontab -e * * * * * /home/steve/cron.pl > /home/steve/cron.out

        That prints "/root" to the 'cron.out' file.

        Set up a crontab for my personal user that does the same thing, and cron.out will contain '/home/steve'. If the OP puts something like this into the crontab of the user calling the real script, it'll identify the issue immediately.

        There are always two common problems when trying to run a script from cron. The first is that cron does not have environment variables set (unless you do so explicitly in the crontab), so it has no knowledge of PATH. This means that you must specify a full path to the script cron is trying to call. The other common issue is what we're seeing in the OP's post (from what I can tell), where cron can execute the script, but the location it is being run out of is not what is expected.

        Using full paths is the easiest way to get cron-run scripts to do the right thing. If one does need to use a relative path, it must be relative to the home directory of the user who's crontab is running the script.

Re: Perl module path not found
by toolic (Bishop) on Oct 07, 2015 at 13:24 UTC
    Your cronjob (which is using Perl version 5.14.2) might be using a different version of Perl. What version are you running when your "code runs fine"?
    perl -v
    You might need to add the full path to the "fine" version in your crontab.

    See also:

Re: Perl module path not found
by RichardK (Parson) on Oct 07, 2015 at 13:57 UTC

    use lib '..'; says use the parent of the CWD, so what is the current directory when you call the script?

    _If_ the script is run from /cgi-bin the you're setting the lib path to '/' but your module is in /cgi-bin/lib, so there's no wonder that you can't find it.

    try use lib 'lib';

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-19 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found