Georgi Milanov
Georgi Milanov

Reputation: 21

How to set up envoirment variables for a perl script in crontab properly? Script is executing with workarounds

Sorry for the long question but I'm bit lost please help me out. I've got the following problem:

OS: Red Hat Enterprise Linux Server release 6.6 (Santiago)

Perl: This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi

Module in question: DBD::Oracle

Quick description:

I have an perl script that runs just fine from the command line. When setting it up in crontab like this:

* * * * * \path\to\script.pl

the following error is generated :

Can't load '/usr/local/lib64/perl5/auto/DBD/Oracle/Oracle.so' for module DBD::Oracle: libclntsh.so.12.1: cannot open shared object file: No such file or directory at /usr/lib64/perl5/DynaLoader.pm line 190.

So far this is what i have tried :

Setting up this block in the begging of the script - same error for crontab execution.

BEGIN {
    $ENV{'ORACLE_HOME'}='/opt/oracli/app/oracli/product/12.2.0/client_1';
    $ENV{'LD_LIBRARY_PATH'}='/opt/oracli/app/oracli/product/12.2.0/client_1/lib:/lib:/usr/lib';
}

Adding the following lines to /home/user/.bash_profile (same crontab user)

ORACLE_HOME=/opt/oracli/app/oracli/product/12.2.0/client_1; export ORACLE_HOME
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH

Correcting does lines in /home/user/.bashrc (because they were messed up there)

Re-loading both .bash_profile & .bashrc

Adding this in the begging of my crontab:

BASH_ENV=/home/user/.bash_profile

All of the above didn't help me. My source was google and working under the assumption that I'm not providing the ORACLE_HOME & LD_LIBRARY_PATH.

When i run a simple shell script that contains :

#!/bin/sh

ORACLE_HOME=/opt/oracli/app/oracli/product/12.2.0/client_1
export ORACLE_HOME
LD_LIBRARY_PATH=/opt/oracli/app/oracli/product/12.2.0/client_1/lib
export LD_LIBRARY_PATH
/path/to/script.pl

Everythign is working just fine so...

Please point me in what am I missing. Is there a different config file in linux for the crontab envoirment.

Sorry for the long post I'm not just that into the OS.

Edit : Sorry i missed this - i want to bypass if possible shell exporting the variables needed invoking the perl :

* * * * * \path\to\shell_with_variables.sh

But to find where I can add the variables globaly so they are visible from crontab executions

Thank you in advance.

Upvotes: 2

Views: 1018

Answers (2)

ikegami
ikegami

Reputation: 385917

LD_LIBRARY_PATH needs to be set before the program (perl) starts.


You can use

ORACLE_HOME=...
LD_LIBRARY_PATH=...

* * * * * /path/to/script.pl

or

* * * * * ORACLE_HOME=... LD_LIBRARY_PATH=... /path/to/script.pl

The first sets it for all cron jobs. The latter for just that one program.


A third option is to fix the environment and relaunch perl.

BEGIN {
   if (@ARGV && $ARGV[0] eq '!fixed!') {
      shift(@ARGV);
   } else {
      $ENV{ ORACLE_HOME     } = "...";
      $ENV{ LD_LIBRARY_PATH } = "...";
      exec($^X, "--", $0, '!fixed!', @ARGV)
         or die("exec: $!\n");
   }
}

Upvotes: 4

Matt Shin
Matt Shin

Reputation: 434

You should add the environment variables at the top of the crontab file itself.

ORACLE_HOME=/opt/oracli/app/oracli/product/12.2.0/client_1
LD_LIBRARY_PATH=/opt/oracli/app/oracli/product/12.2.0/client_1/lib
* * * * * /path/to/script.pl

Alternatively...

As some of the other comments have suggested, if you add environment variables in ~/.bashrc, cron will not pick them up. You can, however, use a bash wrapper to call your Perl.

#!/bin/bash -l
exec /path/to/script.pl

Your crontab file should then call the wrapper instead of your Perl program.

See also crontab(5).

Upvotes: 3

Related Questions