http://www.perlmonks.org?node_id=919509

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

Hi Monks
My script is working fine in ksh shell command prompt when i ran it as manual.
#!/data/opt/pkg/bin/perl use strict; use warnings; use POSIX; use Data::Dumper; require DBI; require DBD::Oracle; ... ...

but if i schedule it in crontab then i am getting below error.

Can't load '/data/opt/pkg/lib/perl5/site_perl/5.10.0/x86_64-linux-thre +ad-multi/auto/DBD/Oracle/Oracle.so' for module DBD::Oracle: libclntsh +.so.11.1: cannot open shared object file: No such file or directory a +t /data/opt/pkg/lib/perl5/5.10.0/x86_64-linux-thread-multi/DynaLoader +.pm line 203.

Also i tried to set environment variable. eventhough its not working from crontab.

$ENV{'ORACLE_HOME'} = "/data/oracle64/product/11.1.0/client_1"; $ENV{'LD_LIBRARY_PATH'} = $ENV{'ORACLE_HOME'}."/lib:".$ENV{'LD_LIBRARY +_PATH'};

kindly suggest me in correct direction. Thanks in advance

Regards,
Velusamy R.


eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Replies are listed 'Best First'.
Re: setting environment variable issue
by ikegami (Patriarch) on Aug 09, 2011 at 20:54 UTC

    Option 1:

    Before any use of DBI/DBD,

    BEGIN { $ENV{ORACLE_HOME} = "..."; $ENV{LD_LIBRARY_PATH} = "..."; }

    I think I heard this doesn't work. Something about a clone of the environment getting creating by the C runtime library before this, and this is what Oracle uses?

    Option 2:

    Set the environment before calling Perl.

    ORACLE_HOME=... LD_LIBRARY_PATH=... perl ...

    You could use a loader script for this.

    Option 3:

    Make the Perl script itself the loader script.

    BEGIN { my $oracle_home = "..."; my $ld_library_path = "..."; if ($ENV{ORACLE_HOME} ne $oracle_home || $ENV{LD_LIBRARY_PATH} ne $ld_library_path ) { $ENV{ORACLE_HOME} = "..."; $ENV{LD_LIBRARY_PATH} = "..."; exec { $^X } $^X, $0, @ARGV; } }

      Oh thank you thank you thank you!!! Option 3 worked great!
      - J
Re: setting environment variable issue
by runrig (Abbot) on Aug 09, 2011 at 18:08 UTC
    Write a shell wrapper to execute your perl, and set ORACLE_HOME before you execute the perl.

      Oracle libraries are loaded when perl is already running1, while loading DBD::Oracle. So you can modify %ENV inside Perl, as long as you do so before loading DBD::Oracle. Typically, you would use a BEGIN block in the main script, before the use DBI / use DBD::Oracle lines.

      Alexander

      1) Exception to the rule: ancient oraperls, that link perl and Oracle code into a single executable

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: setting environment variable issue (crontab)
by toolic (Bishop) on Aug 09, 2011 at 17:34 UTC
    When my script uses non-Core Perl modules, I need to use the full path to the perl executable in my crontab file:
    * * * * * /data/opt/pkg/bin/perl script.pl
    You may also need to add the module path using -I:
    * * * * * /data/opt/pkg/bin/perl -I/data/oracle64/product/11.1.0/clien +t_1/lib script.pl

      Actual problem is to set the Oracle client library path not perl library path. "oracle.so" file is present and its load into my script. Only think is I can't able to connect.

      Oracle client configured properly in unix. Perl script is working fine in command prompt.

      Through crontab only its not working. Can you please suggest me any good way to do it?

      Regards,
      Velusamy R.


      eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: setting environment variable issue
by duyet (Friar) on Aug 09, 2011 at 19:26 UTC

    Try putting the path to your libs before the script and put it in the crontab:

    ORACLE_HOME=/data/oracle64/product/11.1.0/client_1;LD_LIBRARY_PATH=${O +RACLE_HOME}/lib:${LD_LIBRARY_PATH};/full/path/to/your/script.pl
    You might need to expand the ${LD_LIBRARY_PATH} before using it
Re: setting environment variable issue
by kcott (Archbishop) on Aug 10, 2011 at 05:28 UTC

    Here's another option for setting environment variables: this time within the cron file.

    Checking crontab(5), I find:

    $ man 5 crontab ... EXAMPLE CRON FILE # use /bin/sh to run commands, no matter what /etc/passwd says SHELL=/bin/sh # mail any output to `paul', no matter whose crontab this is MAILTO=paul # # run five minutes after midnight, every day 5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1 ...

    -- Ken

Re: setting environment variable issue
by Anonymous Monk on Mar 28, 2014 at 09:07 UTC
    Error is due to missing environment variables . You might have set the environment variables from one SHELL say bash and running the crons from another SHELL say sh.. Srinidhi