1

I have the exact problem described here: Perl can't find module when run from cron.daily except that my problem applies to a perl script running from crontab.

In my case the error message is:

May 24 22:12:02 trackcam3 test_cron: Can't locate Image/Grab.pm in @INC (you may need to install the Image::Grab module) (@INC contains: /etc/perl /usr/local/lib/arm-linux-gnueabihf/perl/5.24.1 /usr/local/share/perl/5.24.1 /usr/lib/arm-linux-gnueabihf/perl5/5.24 /usr/share/perl5 /usr/lib/arm-linux-gnueabihf/perl/5.24 /usr/share/perl/5.24 /usr/local/lib/site_perl /usr/lib/arm-linux-gnueabihf/perl-base) at /home/darren/upload_image.pl line 33, <DATA> line 1.
May 24 22:12:02 trackcam3 test_cron: BEGIN failed--compilation aborted at /home/darren/upload_image.pl line 33, <DATA> line 1.

The solutions at the link all add something to the path. I would like to know if I can move or copy the modules to somewhere they can be found when perl scripts from crontab.

As part of troubleshooting I have already loaded cron with the same PATH as I have from the terminal but that is not enough to allow the perl module to be found.

The missing module is in ~/perl5/lib/perl5 which is not in @INC

The same perl script call modules that are located at

/usr/lib/arm-linux-gnueabihf/perl5/5.24/Image/Magick

Should it be somewhere else? At present /usr/lib/perl5 is empty. The OP in the link asked the same question in the link but didn't receive an answer.

dazz
  • 119
  • 2
  • 14
  • 1
    Sounds like you're using a local lib for that. What user is running the cron job? If it's your own, it should read your bashrc file and pick up the path. But if it's not your own, it should really not require modules that are in your home directory. That's not where it is supposed to be looking. – simbabque May 24 '18 at 10:24
  • 4
    `PATH` is not used for finding modules. Try `PERL5LIB` – Thilo May 24 '18 at 10:30
  • I never intended to load the module in my home dir. That is where it got loaded when installed. It makes more sense to me to have it with the others. Can I move/copy the modules to be with the others without breaking my system? If I move/copy the files, where should they go??? I am the user running the cron job. – dazz May 24 '18 at 10:37
  • 2
    [Crossposted to PerlMonks](https://www.perlmonks.org/?node_id=1215113). – haukex May 24 '18 at 12:36
  • 1
    The root cause of the problem is that most modules were installed as deb packages. These are located in /usr/. A couple of modules were installed with CPAN into the user directory. When the perl scripts were run as user, they worked perfectly. When the perl scripts were run by cron as user, they failed because the perl compiler couldn't find the modules installed by cpan in the user folder. I fixed the problem by running "sudo cpan module" That put the modules where perl can find them. – dazz May 24 '18 at 22:42

2 Answers2

0

Try:

use lib glob( '~/perl5/lib/perl5' );
use Image::Grab;

The use lib must come before many use module.

shawnhcorey
  • 3,545
  • 1
  • 15
  • 17
-2

Follow the link in the following comment by @haukex to arrive at a full explanation.

dazz
  • 119
  • 2
  • 14
  • Here is some more information. https://stackoverflow.com/questions/32726324/i-installed-a-module-successfully-with-cpan-but-perl-cant-find-it-why – dazz May 27 '18 at 09:11
  • *"The root cause of the problem is that cpan doesn't update the @INC environment variable to tell the compiler where to find the modules."* Sorry, but this is incorrect, `@INC` is not an environment variable and it's not `cpan`'s job to modify `@INC` or the `PERL5LIB` environment variable. [I explained in detail over on PerlMonks](https://www.perlmonks.org/?node_id=1215276). – haukex May 27 '18 at 15:47