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,
-- -- GhodmodeBlessed is he who has found his work; let him ask no other blessedness.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unix environment variables for exec process
by pc88mxer (Vicar) on Mar 13, 2008 at 14:56 UTC | |
|
Re: Unix environment variables for exec process
by GhodMode (Pilgrim) on Mar 13, 2008 at 16:46 UTC | |
|
Re: Unix environment variables for exec process
by eyepopslikeamosquito (Archbishop) on Mar 13, 2008 at 20:14 UTC |