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

I installed a perl module manually for my Perl script. Now everytime I run the script from the terminal using "perl script.pl" it runs perfect and I get the output. But everytime I run the script using a cron job it gives me this error

Can't locate GD/Graph/hbars.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 .)

I even tried creating a shell script and even when I run the shell script through the terminal using "sh shellscript.sh" (This script executes the perl script) It works, but everytime I execute this script using a cronjob I get the same error.

Replies are listed 'Best First'.
Re: Perl @INC Error (cron)
by toolic (Bishop) on Aug 15, 2013 at 15:00 UTC
    In your shell, run: which perl

    In your crontab, list the full path to your perl executable:

    /full/path/to/my/perl script.pl

      Hey thanks for the reply. I tried your method it worked, however now I get a compilation error. When I run the script in terminal using "perl script.pl" it runs fine and gives me the output, however now it's giving me a error "No data sets or points at /path/script.pl line 142"

Re: Perl @INC Error
by tobyink (Canon) on Aug 15, 2013 at 15:01 UTC

    cron is likely to run under a different user account, with different file permissions and different environment variables.

    Check which user cron is running under and make sure they have permission to read GD/Graph/hbars.pm, as well as read and execute permission on the directories "above" that on the filesystem.

    See if you can find any differences in the environment variables between your account and cron's account, especially the PERL5LIB and PERL5OPT variables.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      To follow up on your suggestion, I recommend that zee3b put the following in a cron job:
      * * * * * echo $PERL5LIB * * * * * echo $PERL5OPT

      And if one isn't as expected just set the appropriate variable in crontab (but above any cron jobs).

        Another solution is to call the script in the cron this way:

        bash -l -c "perl /path/to/script.pl"

        With that you assure that the environment is build like the one you have on the console, where you tested your perl script.

        McA