Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl can't find module when run from cron.daily

I have perl programs that use Net::Finger and have run successfully from cron.daily in Fedora 11.
I just upgraded a server to Fedora 18 and these same perl programs no longer run from cron but run fine from command line when logged in as root.
The error is:

Can't locate Net/Finger.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .)

The path to the module is /root/perl5/lib/perl5/Net/Finger.pm but I can't figure out how to add the path without causing more errors. Thanks in advance.

like image 999
Xi Vix Avatar asked Jun 19 '13 14:06

Xi Vix


People also ask

How do I find out where a Perl module is installed?

Step 1: Instantiate the module in your script... Step 2: Execute the script with the Perl graphical debugger... Step 3: Step in to the new call. The full pathname of the module will be displayed on the title-bar of the debugger window.

How do I know if my cron job is working?

Using Output In a Script to Show a Running Cron Job You can add a line of code in your existing script to output a result when the script is run. If the result of this command produces an output, then you can use this output to confirm that your cron script is running.

Where do Cronjobs run from?

Cron jobs are typically located in the spool directories. They are stored in tables called crontabs. You can find them in /var/spool/cron/crontabs. The tables contain the cron jobs for all users, except the root user.


1 Answers

See perlfaq8.

Here are three ways to add arbitrary directories to Perl's module search path.

  1. Set the PERL5LIB environment variable

    15 15 * * 1-5 PERL5LIB=/root/perl5/lib/perl5 /usr/local/bin/perl my_script.pl
    
  2. Use the -I command line switch

    15 15 * * 1-5 /usr/local/bin/perl -I/root/perl5/lib/perl5 my_script.pl
    
  3. Use the lib pragma inside your perl script

    #! /usr/local/bin/perl
    # my_script.pl: the script that does my thing
    use lib '/root/perl5/lib/perl5';
    use Net::Finger;
    ...
    

Also note that the environment of a cron job is much sparser than the environment of your command line, and in particular the cron environment's $PATH variable might not be what you expect. If you're not specifying the full path to the Perl executable, verify what $PATH the cron environment is using and make sure you are running the right version of perl.

like image 155
mob Avatar answered Nov 15 '22 07:11

mob