7

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.

Jorge V.
  • 172
  • 6
Xi Vix
  • 1,381
  • 6
  • 24
  • 43

2 Answers2

16

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.

mob
  • 117,087
  • 18
  • 149
  • 283
  • Thanks. This program also uses DBI ... which perl has no problem finding from cron so it's probably in a different place. If I use the lib with path will it still be able to find DBI ? In other words, does it add the specified library to the existing environment or replace it ? – Xi Vix Jun 19 '13 at 15:37
  • All of these methods add (prepend, really) to the search path. If you had another module called `DBI.pm` under `/root/perl5/lib/perl5`, then perl would load that one instead of the system one. – mob Jun 19 '13 at 15:41
  • This question/answer saved me. – Rocky Nov 13 '15 at 04:00
1

What happens if you add this to the top of the script?

#!/usr/bin/perl
use lib "/root/perl5/lib/perl5/Net";

Was this previously set in the .profile?

This is from a ubuntu thread, but may be related: https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work *Cron passes a minimal set of environment variables to your jobs. To see the difference, add a dummy job like this:

* * * * * env > /tmp/env.output*

I don't have a linux box handy at the moment, but I would try to see what's different in the @INC by running the following logged in as root and trying to set up a job to see what the crontab has. The env output above may indicate a different shell is being used by crontab, than what you may have set when logging in as root.

log in as root and run:

perl -le 'print for @INC'

set up a cron job to see the differences. I'm not sure if this syntax will work, but you could put the command in a simple pl script.

* * * * * perl -le 'print for @INC' > /tmp/inc.output

do the same for env

Community
  • 1
  • 1
  • perl -le 'print for @INC' returns: `/root/perl5/lib/perl5/x86_64-linux-thread-multi /root/perl5/lib/perl5 /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 cron jobs did not produce any output in /tmp – Xi Vix Jun 19 '13 at 15:40
  • I see why the cron jobs did not produce any output ... got "bad username" error in cron log. What username did you want me to use? – Xi Vix Jun 19 '13 at 18:59
  • I tried entering root as the username in the crontab and got this: `[root@sendmail tmp]# cat env.output MAILTO=root SHELL=/bin/bash USER=root PATH=/sbin:/bin:/usr/sbin:/usr/bin PWD=/root SHLVL=1 HOME=/root LOGNAME=root _=/bin/env [root@sendmail tmp]# cat inc.output /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5` – Xi Vix Jun 20 '13 at 02:02