Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Perl app won't compile /run from cron

by dazz (Beadle)
on May 23, 2018 at 21:06 UTC ( [id://1215113]=perlquestion: print w/replies, xml ) Need Help??

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

Hi I have a perl script that runs OK from the terminal but won't run from cron. I get a logged error that looks like this:
May 24 09:00:02 trackcam3 test_cron: Can't locate Image/Grab.pm in @IN +C (you may need to install the Image::Grab module) (@INC contains: /e +tc/perl /usr/local/lib/arm-linux-gnueabihf/perl/5.24.1 /usr/local/sha +re/perl/5.24.1 /usr/lib/arm-linux-gnueabihf/perl5/5.24 /usr/share/per +l5 /usr/lib/arm-linux-gnueabihf/perl/5.24 /usr/share/perl/5.24 /usr/l +ocal/lib/site_perl /usr/lib/arm-linux-gnueabihf/perl-base) at /home/d +arren/upload_image.pl line 33, <DATA> line 1. May 24 09:00:02 trackcam3 test_cron: BEGIN failed--compilation aborted + at /home/darren/upload_image.pl line 33, <DATA> line 1.
So I ran:
darren@trackcam3:~ $ cpan Image::Grab Loading internal null logger. Install Log::Log4perl for logging messag +es Reading '/home/darren/.cpan/Metadata' Database was generated on Wed, 23 May 2018 19:54:03 GMT Image::Grab is up to date (1.4.2).
This looks like cron doesn't have the right path in the environment
The path statement in crontab looks like:
PATH=/home/darren/perl5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin +:/usr/bin:/sbin:/bin # PATH =/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
The uncommented path is the same as the one in my user profile. The commented path is the one that has worked for years.
This problem started when I build a new server from scratch using a documented procedure. Something I have done quite a few times. Something new has happened during the build. I don't know what that was and I don't know how to solve this problem.

Dazz

Replies are listed 'Best First'.
Re: Perl app won't compile /run from cron
by haukex (Archbishop) on May 23, 2018 at 21:48 UTC

    Try the command perldoc -l Image::Grab to show you where the module got installed, I'm guessing that that path isn't one of the @INC directories listed at the top of your post. Setting the right PATH isn't always enough if you've got custom entries added to @INC via something like the PERL5LIB environment variable, because then that environment variable needs to be set in the environment that cron provides to the programs it runs. I'm still just guessing, but probably that isn't the case and the environment provided by cron is different from the one you have on the command line. For example, are you using local::lib? If you want to see the difference, try running this command from both the command line and from a crontab entry: perl -le 'die "$ENV{PERL5LIB} @INC\n"'

    If those are different, then it depends on how you're changing @INC for the best way to solve this. If you could report back with the results of the above, we can probably help more.

      Hi I tried  perldoc -l Image::Grab but I don't have perldoc installed.
      I am not using local::lib.
      I used the cron commmand
      * * * * * /usr/bin/perl /home/user/perl_script.pl 1 2>&1 | /usr/bin/ +logger -t test_cron
      to get the compiler error output from syslog.

      Dazz
        I am not using local::lib.

        Based on the pathname ~/perl5/lib/perl5 and that you say you used cpan to install the module, I'm pretty sure you are using local::lib. The cpan client, when run without sudo, will usually detect that it doesn't have permissions to write to the system directories and autoconfigure to use local::lib. If you check your .profile or .bashrc files, you should see variables like PERL_LOCAL_LIB_ROOT and PERL5LIB being set there, these would have been added there by local::lib, and as I said those are the variables that are probably missing from the environment that cron provides, hence my suggestion to try and set PERL5LIB explicitly.

        I tried perldoc -l Image::Grab but I don't have perldoc installed.

        Well, you could do sudo apt-get install perl-doc, but did you try dave_the_m's suggestion, which should work even without perldoc? I also made a few other suggestions and asked a few questions above as well as here.

        I tried perldoc -l Image::Grab but I don't have perldoc installed.
        locate Image/Grab.pm
Re: Perl app won't compile /run from cron
by dave_the_m (Monsignor) on May 23, 2018 at 21:49 UTC
    Perl usually gets extra module paths from the PERL5LIB environment variable rather than PATH. So that would be the first thing to look at.

    Also, its possible that cron and the terminal are running as different users (and so with different file/directory permissions), or are using different versions of the perl executable. You can see where the module is being loaded from the terminal by using:

    $ perl -le 'require Image::Grab; print $INC{"Image/Grab.pm"}' /some/path/lib/Image/Grab.pm
    If that path isn't in the list of paths in the cron error message, that's another place to investigate.

    Dave.

      ... or are using different versions of the perl executable.

      Excellent points, and that reminds me of another piece of advice I've learned the hard way (a few times ;-) ): Always use absolute paths in crontab.

Re: Perl app won't compile /run from cron
by choroba (Cardinal) on May 23, 2018 at 22:20 UTC
    Perl modules are searched for in @INC, not $ENV{PATH}. To set @INC, you can use the $PERL5LIB environment variable, or specify the directories with the -I options to perl (see perlrun).
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Perl app won't compile /run from cron
by haukex (Archbishop) on May 24, 2018 at 12:37 UTC

    Crossposted to StackOverflow. Crossposting is acceptable, but it is considered polite to inform about it so that efforts aren't duplicated.

Re: Perl app won't compile /run from cron
by Random_Walk (Prior) on May 24, 2018 at 08:44 UTC

    It looks like you are missing the path to your library in your @INC array within Perl. You can add a line to your script to explicitly use the library directory where Image::Grab is installed.

    use lib '/var/local/perl-site/';
    Or where ever it may be. It is strange that it is not in a standard system patch though if it is installed with cpan. Do you know the directory where Grab.pm is installed?

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
      Hi Grab is located:
      darren@trackcam3:~ $ find ~/ -name Grab /home/darren/.cpan/build/Image-Grab-1.4.2-gwMQYz/lib/Image/Grab /home/darren/.cpan/build/Image-Grab-1.4.2-gwMQYz/blib/lib/Image/Grab /home/darren/.cpan/build/Image-Grab-1.4.2-gwMQYz/blib/lib/auto/Image/G +rab /home/darren/.cpan/build/Image-Grab-1.4.2-gwMQYz/blib/arch/auto/Image/ +Grab /home/darren/perl5/lib/perl5/arm-linux-gnueabihf-thread-multi-64int/au +to/Image/Grab /home/darren/perl5/lib/perl5/Image/Grab
      Image::Magick is installed here:
      /usr/lib/arm-linux-gnueabihf/perl5/5.24/Image/Magick /usr/lib/arm-linux-gnueabihf/perl5/5.24/auto/Image/Magick
      This is a headless server so having libraries splattered around isn't helpful. Looking back at my notes, CPAN installed the modules in my home dir. The Ubuntu packages were installed in /usr.

      Dazz

        "This is a headless server so having libraries splattered around isn't helpful"

        Then don't do that. If you are using the system perl and a package is available in apt, use that, otherwise you need to install things in the right place (sudo cpanm Image::Grab), or (as detailed in other replies you already have) specify the correct paths for your cron jobs for where you have installed things.

Re: Perl app won't compile /run from cron
by marto (Cardinal) on May 24, 2018 at 08:46 UTC

    You've posted this many times now, I suspect you are refreshing your screen and resubmitting a form. Your first post has responses.

      Hi I have been refreshing my screen looking for responses. I had no idea a refresh caused a resubmit. I have only just found this message and the responses. I don't visit this site very often so still learning its quirks.

      Dazz

      I suspect you are refreshing your screen and resubmitting a form.

      As users have been trained to do. pmdev should simply use POST/REDIRECT/GET like they're supposed to

Re: Perl app won't compile /run from cron
by dazz (Beadle) on May 24, 2018 at 09:43 UTC
    Hi The problem I have is exactly the same as this link
    https://stackoverflow.com/questions/17194632/perl-cant-find-module-when-run-from-cron-daily
    I have found the Image::Grab module at
    ~/perl5/lib/perl5/Image
    The directory:
    :/usr/lib/perl5
    is empty and so is
    :/usr $ ls lib/perl5/
    I am running Rasbian on a Raspberry Pi. I don't know what files are supposed to be where.

    Dazz

      Sounds like the module was installed with local::lib, which should normally set its environment variables in .profile/.bashrc. Anyway, did you try the solutions suggested in that link? For example prepending PERL5LIB=/home/pi/perl5/lib/perl5 to the command in your crontab?

        Hi
        The "missing" modules were installed with CPAN that has chosen to put them in my home dir.

        Dazz

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found