sudo perl script with environment variables on linux

Discussion in 'Perl Misc' started by powah, Apr 22, 2009.

  1. powah

    powah Guest

    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 =
    powah, Apr 22, 2009
    #1
    1. Advertising

  2. powah

    smallpond Guest

    On Apr 22, 12:18 pm, 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 =


    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.
    smallpond, Apr 22, 2009
    #2
    1. Advertising

  3. powah

    Nathan Keel Guest

    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.
    Nathan Keel, Apr 22, 2009
    #3
  4. powah

    smallpond Guest

    On Apr 22, 2:59 pm, Nathan Keel <> 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".
    smallpond, Apr 22, 2009
    #4
  5. powah

    Nathan Keel Guest

    smallpond wrote:

    > On Apr 22, 2:59 pm, Nathan Keel <> 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?
    Nathan Keel, Apr 22, 2009
    #5
  6. powah

    smallpond Guest

    On Apr 22, 5:17 pm, Nathan Keel <> wrote:
    > smallpond wrote:
    > > On Apr 22, 2:59 pm, Nathan Keel <> 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.
    smallpond, Apr 22, 2009
    #6
  7. powah

    kun niu Guest

    On 4ÔÂ23ÈÕ, ÉÏÎç12ʱ18·Ö, 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 =


    How about trying the following command:
    sudo env JAVA_HOME=$JAVA_HOME perl testenv.pl
    kun niu, Apr 23, 2009
    #7
  8. powah

    kun niu Guest

    On 4ÔÂ23ÈÕ, ÉÏÎç12ʱ18·Ö, 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 =


    Or you can try to edit the /etc/sudoers file and add the following
    line:
    powah ALL = SETENV: ALL
    If you have the permission.
    kun niu, Apr 23, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kent Tenney

    Trying to run a sudo command from script

    Kent Tenney, Jan 1, 2010, in forum: Python
    Replies:
    2
    Views:
    1,242
    Kent Tenney
    Jan 3, 2010
  2. Paul Kölle
    Replies:
    1
    Views:
    395
  3. Trans
    Replies:
    2
    Views:
    92
    Trans
    May 20, 2008
  4. Une Bévue

    testing if a script is run via sudo

    Une Bévue, Oct 7, 2010, in forum: Ruby
    Replies:
    6
    Views:
    101
    Une Bévue
    Oct 7, 2010
  5. J. Romano
    Replies:
    5
    Views:
    1,469
    Darren Dunham
    Jul 7, 2004
Loading...

Share This Page