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

Fellow Monks,
How can I set Unix environment variables that can be read by an exec'd process?

I'm trying to automate a backup process and I'm using Expect for some of the commands that prompt for information. This works perfectly when I execute it from the command line, but when I execute it from cron, I get something like the following error in the cron output:

Cannot exec(exp full=y statistics=none file=/home/ghodmode/runBackup/b +ackup/13Mar2008_020502/oracle_data_13Mar2008_020502.dmp): No such fil +e or directory

I realize this is probably related to the Unix PATH. The processes I'm trying to run need many other special environment variables set, too. I tried to take care of this by setting the variables via the %ENV hash before the spawn, but it's not working.

I've read Loading Environment Variables from a File, but that doesn't exactly meet my needs because I need to execute many processes with requirements defined in an external configuration file.

Until I figure this out, I'll use a shell script to set up the necessary environment variables and call the command, then run that script with expect, but I'd prefer an all-Perl solution.

Here's an example of the code that presents the problem:

#!/usr/bin/perl -w use strict; use warnings; use Expect; my $envscript = '/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/b +in/oracle_env.sh'; open(ENV, "$envscript 2>&1 > expect.out; env |"); while ( <ENV> ) { chomp; my ($var, $value) = split( /=/, $_, 2 ); next if ( !defined $var or !defined $value ); $ENV{$var} = $value; } close ENV; my $command = 'exp'; my @parameters = ( 'full=y', 'statistics=none', 'file=/home/ghodmode/runBackup/backup/oracle_data.dmp', ); my $patidx; my $pattern; my $exp = new Expect; $exp->raw_pty(1); $exp->spawn($command, @parameters) or die "Cannot spawn $command: $!\n"; $pattern = '^Username:'; $patidx = $exp->expect( 5, ( '-re', $pattern) ); $exp->send( "me\n" ); $pattern = '^Password:'; $patidx = $exp->expect( 5, ('-re', $pattern) ); $exp->send( "my_password\n" ); $exp->soft_close();

Thank you,

--
-- Ghodmode
Blessed is he who has found his work; let him ask no other blessedness.
-- Thomas Carlyle

Replies are listed 'Best First'.
Re: Unix environment variables for exec process
by pc88mxer (Vicar) on Mar 13, 2008 at 14:56 UTC
    Try specifying the full path to the exp command, e.g.:
    my $command = '/full/path/to/exp';
    If you don't know where it is, just use the which command from your terminal:
    $ which exp
    Update: This is very likely a PATH issue. When you run the command from the command line your PATH variable contains the directory that the exp resides in. When cron runs it begins with a very limited PATH. Even though you are modifying environement variables before invoking exp, note that it is being run in cron's context. So if oracle_env.sh does not explicitly modify the PATH variable to include the directory for exp, cron will not be able to find that program.
Re: Unix environment variables for exec process
by GhodMode (Pilgrim) on Mar 13, 2008 at 16:46 UTC

    I think I found the problem. I probably should have read Loading Environment Variables from a File more carefully, too.

    When I started to write the shell script that would set up the environment and run the command, I realized that I needed to source the environment set-up script, not just run it.

    I put source in front of my environment script and now it's running from cron and seems to be working ok.

    Thank you,

    --
    -- Ghodmode
    
    Blessed is he who has found his work; let him ask no other blessedness.
    -- Thomas Carlyle
Re: Unix environment variables for exec process
by eyepopslikeamosquito (Archbishop) on Mar 13, 2008 at 20:14 UTC

    Probably not related to your problem, but I noticed:

    $envscript 2>&1 > expect.out;
    Assuming you want to fold stdout/stderr into file expect.out, you've got the order wrong. It should be written as:
    $envscript > expect.out 2>&1;
    To see what I mean, run the following commands:
    perl -le "print STDERR q{to-stderr};print STDOUT q{to-stdout}" 2>&1 >f +.tmp perl -le "print STDERR q{to-stderr};print STDOUT q{to-stdout}" >f.tmp +2>&1
    and note the difference.