Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > sudo perl script with environment variables on linux

Reply
Thread Tools

sudo perl script with environment variables on linux

 
 
powah
Guest
Posts: n/a
 
      04-22-2009
How to use sudo to run a perl script with the environment variable
JAVA_HOME on linux?
Running testenv.pl as sudo do not pickup the environment variable
JAVA_HOME
value.

$ echo $JAVA_HOME
/home/powah/jdk1.6.0_13

testenv.pl is:
#!/usr/bin/perl
print "begin JAVA_HOME = $JAVA_HOME\n";
$J_HOME = $ENV{'JAVA_HOME'};
print "$ENV{'JAVA_HOME'} JAVA_HOME = $J_HOME\n";

$ perl testenv.pl
begin JAVA_HOME =
/home/powah/jdk1.6.0_13 JAVA_HOME = /home/powah/jdk1.6.0_13

$ sudo perl testenv.pl
begin JAVA_HOME =
JAVA_HOME =
 
Reply With Quote
 
 
 
 
smallpond
Guest
Posts: n/a
 
      04-22-2009
On Apr 22, 12:18 pm, powah <(E-Mail Removed)> wrote:
> How to use sudo to run a perl script with the environment variable
> JAVA_HOME on linux?
> Running testenv.pl as sudo do not pickup the environment variable
> JAVA_HOME
> value.
>
> $ echo $JAVA_HOME
> /home/powah/jdk1.6.0_13
>
> testenv.pl is:
> #!/usr/bin/perl
> print "begin JAVA_HOME = $JAVA_HOME\n";
> $J_HOME = $ENV{'JAVA_HOME'};
> print "$ENV{'JAVA_HOME'} JAVA_HOME = $J_HOME\n";
>
> $ perl testenv.pl
> begin JAVA_HOME =
> /home/powah/jdk1.6.0_13 JAVA_HOME = /home/powah/jdk1.6.0_13
>
> $ sudo perl testenv.pl
> begin JAVA_HOME =
> JAVA_HOME =


perl -e 'print sort join " ",sort keys %ENV'
CVSROOT CVS_RSH DISPLAY EDITOR G_BROKEN_FILENAMES HISTSIZE HOME
HOSTNAME INPUTRC LANG LESSOPEN LOGNAME MAIL OLDPWD PATH PWD SHELL
SHLVL SSH_CLIENT SSH_CONNECTION SSH_TTY TERM USER _

sudo perl -e 'print sort join " ",sort keys %ENV'
DISPLAY HISTSIZE HOME HOSTNAME INPUTRC LANG LOGNAME MAIL PATH SHELL
SUDO_COMMAND SUDO_GID SUDO_UID SUDO_USER TERM USER

sudo seems to change or unset many environment variables.
This is not a perl issue -- perl just tells what it sees.
 
Reply With Quote
 
 
 
 
Nathan Keel
Guest
Posts: n/a
 
      04-22-2009
powah wrote:

> How to use sudo to run a perl script with the environment variable
> JAVA_HOME on linux?
> Running testenv.pl as sudo do not pickup the environment variable
> JAVA_HOME
> value.
>
> $ echo $JAVA_HOME
> /home/powah/jdk1.6.0_13
>
> testenv.pl is:
> #!/usr/bin/perl
> print "begin JAVA_HOME = $JAVA_HOME\n";
> $J_HOME = $ENV{'JAVA_HOME'};
> print "$ENV{'JAVA_HOME'} JAVA_HOME = $J_HOME\n";
>
> $ perl testenv.pl
> begin JAVA_HOME =
> /home/powah/jdk1.6.0_13 JAVA_HOME = /home/powah/jdk1.6.0_13
>
> $ sudo perl testenv.pl
> begin JAVA_HOME =
> JAVA_HOME =


Because powah's environment is not root's.
 
Reply With Quote
 
smallpond
Guest
Posts: n/a
 
      04-22-2009
On Apr 22, 2:59 pm, Nathan Keel <(E-Mail Removed)> wrote:
> powah wrote:
> > How to use sudo to run a perl script with the environment variable
> > JAVA_HOME on linux?
> > Running testenv.pl as sudo do not pickup the environment variable
> > JAVA_HOME
> > value.

>
> > $ echo $JAVA_HOME
> > /home/powah/jdk1.6.0_13

>
> > testenv.pl is:
> > #!/usr/bin/perl
> > print "begin JAVA_HOME = $JAVA_HOME\n";
> > $J_HOME = $ENV{'JAVA_HOME'};
> > print "$ENV{'JAVA_HOME'} JAVA_HOME = $J_HOME\n";

>
> > $ perl testenv.pl
> > begin JAVA_HOME =
> > /home/powah/jdk1.6.0_13 JAVA_HOME = /home/powah/jdk1.6.0_13

>
> > $ sudo perl testenv.pl
> > begin JAVA_HOME =
> > JAVA_HOME =

>
> Because powah's environment is not root's.


FOO=baz
sudo echo $FOO
baz

Note that I did not even need to export FOO.
sudo runs in the existing environment except for variables
it deems "unsafe".
 
Reply With Quote
 
Nathan Keel
Guest
Posts: n/a
 
      04-22-2009
smallpond wrote:

> On Apr 22, 2:59 pm, Nathan Keel <(E-Mail Removed)> wrote:
>> powah wrote:
>> > How to use sudo to run a perl script with the environment variable
>> > JAVA_HOME on linux?
>> > Running testenv.pl as sudo do not pickup the environment variable
>> > JAVA_HOME
>> > value.

>>
>> > $ echo $JAVA_HOME
>> > /home/powah/jdk1.6.0_13

>>
>> > testenv.pl is:
>> > #!/usr/bin/perl
>> > print "begin JAVA_HOME = $JAVA_HOME\n";
>> > $J_HOME = $ENV{'JAVA_HOME'};
>> > print "$ENV{'JAVA_HOME'} JAVA_HOME = $J_HOME\n";

>>
>> > $ perl testenv.pl
>> > begin JAVA_HOME =
>> > /home/powah/jdk1.6.0_13 JAVA_HOME = /home/powah/jdk1.6.0_13

>>
>> > $ sudo perl testenv.pl
>> > begin JAVA_HOME =
>> > JAVA_HOME =

>>
>> Because powah's environment is not root's.

>
> FOO=baz
> sudo echo $FOO
> baz
>
> Note that I did not even need to export FOO.
> sudo runs in the existing environment except for variables
> it deems "unsafe".


I wasn't thinking when I replied before. Anyway, unless it's exported,
it won't exist in $ENV{NAME} for Perl to see, I believe?
 
Reply With Quote
 
smallpond
Guest
Posts: n/a
 
      04-22-2009
On Apr 22, 5:17 pm, Nathan Keel <(E-Mail Removed)> wrote:
> smallpond wrote:
> > On Apr 22, 2:59 pm, Nathan Keel <(E-Mail Removed)> wrote:
> >> powah wrote:
> >> > How to use sudo to run a perl script with the environment variable
> >> > JAVA_HOME on linux?
> >> > Running testenv.pl as sudo do not pickup the environment variable
> >> > JAVA_HOME
> >> > value.

>
> >> > $ echo $JAVA_HOME
> >> > /home/powah/jdk1.6.0_13

>
> >> > testenv.pl is:
> >> > #!/usr/bin/perl
> >> > print "begin JAVA_HOME = $JAVA_HOME\n";
> >> > $J_HOME = $ENV{'JAVA_HOME'};
> >> > print "$ENV{'JAVA_HOME'} JAVA_HOME = $J_HOME\n";

>
> >> > $ perl testenv.pl
> >> > begin JAVA_HOME =
> >> > /home/powah/jdk1.6.0_13 JAVA_HOME = /home/powah/jdk1.6.0_13

>
> >> > $ sudo perl testenv.pl
> >> > begin JAVA_HOME =
> >> > JAVA_HOME =

>
> >> Because powah's environment is not root's.

>
> > FOO=baz
> > sudo echo $FOO
> > baz

>
> > Note that I did not even need to export FOO.
> > sudo runs in the existing environment except for variables
> > it deems "unsafe".

>
> I wasn't thinking when I replied before. Anyway, unless it's exported,
> it won't exist in $ENV{NAME} for Perl to see, I believe?


hmmm. No. I was stupid. $FOO is being expanded before calling sudo.

export FOO
sudo perl -e 'print $ENV{"FOO"}'

no output.
So FOO is not in the environment in the sudo command.
 
Reply With Quote
 
kun niu
Guest
Posts: n/a
 
      04-23-2009
On 4月23日, 上午12时18分, powah <(E-Mail Removed)> wrote:
> How to use sudo to run a perl script with the environment variable
> JAVA_HOME on linux?
> Running testenv.pl as sudo do not pickup the environment variable
> JAVA_HOME
> value.
>
> $ echo $JAVA_HOME
> /home/powah/jdk1.6.0_13
>
> testenv.pl is:
> #!/usr/bin/perl
> print "begin JAVA_HOME = $JAVA_HOME\n";
> $J_HOME = $ENV{'JAVA_HOME'};
> print "$ENV{'JAVA_HOME'} JAVA_HOME = $J_HOME\n";
>
> $ perl testenv.pl
> begin JAVA_HOME =
> /home/powah/jdk1.6.0_13 JAVA_HOME = /home/powah/jdk1.6.0_13
>
> $ sudo perl testenv.pl
> begin JAVA_HOME =
> JAVA_HOME =


How about trying the following command:
sudo env JAVA_HOME=$JAVA_HOME perl testenv.pl
 
Reply With Quote
 
kun niu
Guest
Posts: n/a
 
      04-23-2009
On 4月23日, 上午12时18分, powah <(E-Mail Removed)> wrote:
> How to use sudo to run a perl script with the environment variable
> JAVA_HOME on linux?
> Running testenv.pl as sudo do not pickup the environment variable
> JAVA_HOME
> value.
>
> $ echo $JAVA_HOME
> /home/powah/jdk1.6.0_13
>
> testenv.pl is:
> #!/usr/bin/perl
> print "begin JAVA_HOME = $JAVA_HOME\n";
> $J_HOME = $ENV{'JAVA_HOME'};
> print "$ENV{'JAVA_HOME'} JAVA_HOME = $J_HOME\n";
>
> $ perl testenv.pl
> begin JAVA_HOME =
> /home/powah/jdk1.6.0_13 JAVA_HOME = /home/powah/jdk1.6.0_13
>
> $ sudo perl testenv.pl
> begin JAVA_HOME =
> JAVA_HOME =


Or you can try to edit the /etc/sudoers file and add the following
line:
powah ALL = SETENV: ALL
If you have the permission.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Trying to run a sudo command from script Paul K鰈le Python 1 01-14-2010 04:28 AM
Trying to run a sudo command from script Kent Tenney Python 2 01-03-2010 02:05 PM
sudo ruby loosing environment? Trans Ruby 2 05-20-2008 02:01 AM
Setting environment variables from a Perl script J. Romano Perl Misc 5 07-07-2004 04:07 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments