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

i have a perl script that connects to an oracle database and therefor i have modified my .profile file in my home directory (i have added some oracle specific environment variables).

my problem is that the perl script needs to be run using 'cron'. since 'cron' uses only a limited environment i get errors that my perl script cannot connect to the oracle database. is there anyone who knows how you can have a perl script execute the .profile file?

thanks in advance!
arjan huijzer

p.s. i have tried to create the variables in %ENV in a BEGIN block, but somehow this doesn't solve the problem.

Replies are listed 'Best First'.
Re: running .profile
by davorg (Chancellor) on May 02, 2001 at 15:58 UTC

    A non-Perl solution would be to source the .profile in the crontab entry before running the script.

    0 12 * * * . /home/dave/.profile;/path/to/script.pl

    Update: Changed source to .. cron always uses the Bourne shell :(

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      this doesn't seem to work. i have created a simple test script:

      #!/usr/local/bin/perl -w use strict; foreach (keys %ENV) { print "[$_] $ENV{$_}\n"; }

      next i created a .profile file with some oracle environment variables in it.

      when i insert the following line in cron:

      * * * * * source /opt/home/u_huiar1/.profile;/opt/home/u_huiar1/perl/c +ron.pl 1>> /opt/home/u_huiar1/perl/output 2>&1

      the file 'output' contains the following lines:

      [LOGNAME] u_huiar1 [PATH] /usr/bin: [SHELL] /usr/bin/sh [HOME] /home/u_huiar1 [TZ] MET

      somehow it does not have the oracle environment variables in its environment.

      arjan huijzer

        source is particular to the csh shell (maybe bash/zsh/etc). Perhaps you are using ksh or sh. Try:
        * * * * * . /opt/home/u_huiar1/.profile;/opt/home/u_huiar1/perl/c +ron.pl 1>> /opt/home/u_huiar1/perl/output 2>&1
        ksh and sh use "." (the dot command) to source files.
Re: running .profile
by stefan k (Curate) on May 02, 2001 at 16:33 UTC
    AFAIK you can set some variables in your crontab that will be available for the processes started by cron. Like this:
    ORACLE_HOME=/path/to/oracle 20 07 * * 2-6 /home/me/bin/my_perl_script.pl
    I must admit, though, that I have encoutered (linux) systems where this was not working, but on my current it does the job. Think you can configure cron to do so...

    Regards Stefan K

Re (tilly) 1: running .profile
by tilly (Archbishop) on May 03, 2001 at 07:18 UTC
    In a cron I find the cost of running .profile quite acceptable. (Hey, nobody is waiting!) Try Get default login environment for a sample of how to do it. (Works with whatever shell you want, defaulting to your default login shell.)
Re: running .profile
by DeaconBlues (Monk) on May 02, 2001 at 19:11 UTC

    I would not recommend running the whole .profile file for the cron instance. Most times there is more in your profile than the few Oracle environment variables you need. I normally get this to work using a shell script wrapper around my perl script.

    So your cron would call the shell script

    0 12 * * * /path/to/script.sh

    Then your shell script would set the variables and run the script

    #!/bin/sh ORACLE_HOME=/path/to/oracle ORACLE_SID=testdb perl /path/to/script.pl
Re: running .profile
by Anonymous Monk on May 02, 2001 at 18:06 UTC
    Or, you could just use dotsh.pl
    require "dotsh.pl"; $ENV{SHELL} = "/bin/sh"; dotsh(".profile");