sudo perl script with environment variables on linux

P

powah

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 =
 
S

smallpond

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.
 
N

Nathan Keel

powah said:
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.
 
S

smallpond

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".
 
N

Nathan Keel

smallpond said:
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?
 
S

smallpond

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.
 
K

kun niu

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
 
K

kun niu

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top