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

In reply to Unix environment variables for exec process by GhodMode

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.