Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Script works when run with sudo, but not when run directly as root...?

by bmalnad (Initiate)
on May 07, 2014 at 18:46 UTC ( #1085362=perlquestion: print w/replies, xml ) Need Help??
bmalnad has asked for the wisdom of the Perl Monks concerning the following question:

Hey Guys,

I just inherited this Perl script from a former coworker. It works fine if I run it from my account using: sudo perl foo.pgp bar.txt

If I do a sudo su - root and type: perl foo.pgp bar.txt

It fails with this error: Use of uninitialized value $plaintext[0] in print at line 23, <$ciphertext_fh> line 2486.

#!/usr/bin/perl use strict; use warnings; use Crypt::GPG; use Data::Dumper; my $gpg = new Crypt::GPG; $gpg->gpgbin('/usr/bin/gpg'); $gpg->secretkey('xxxxxxxxxx'); $gpg->passphrase('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); my $inputfile = shift; my $outputfile = shift; open my $ciphertext_fh, '<', $inputfile or die "couldn't open $inputfi +le: $!"; open my $output_fh, '>', $outputfile or die "couldn't open $outputfile + for writing: $!"; my @ciphertext = <$ciphertext_fh>; my (@plaintext) = $gpg->decrypt(\@ciphertext) or die "didn't work: $!" +; print $output_fh @plaintext; close $ciphertext_fh; close $output_fh;
  • Comment on Script works when run with sudo, but not when run directly as root...?
  • Download Code

Replies are listed 'Best First'.
Re: Script works when run with sudo, but not when run directly as root...?
by MidLifeXis (Monsignor) on May 07, 2014 at 19:50 UTC

    Which perl is being run? su - root will get you root's login shell along with root's default environment. sudo blah blah blah gets you your environment (notwithstanding any settings in your sudo configuration).

    Try the difference between the env and which perl commands in both environments.


      I can't find anything that makes any sense. dlamb@xxxxxxxxxx:~$ env && which perl
      TERM=xterm SHELL=/bin/bash SSH_CLIENT= 38907 22 SSH_TTY=/dev/pts/0 USER=dlamb MAIL=/var/mail/dlamb PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr +/games PWD=/home/dlamb LANG=en_US.UTF-8 SHLVL=1 HOME=/home/dlamb LANGUAGE=en_US: LOGNAME=dlamb SSH_CONNECTION= 38907 22 LESSOPEN=| /usr/bin/lesspipe %s LESSCLOSE=/usr/bin/lesspipe %s %s _=/usr/bin/env /usr/bin/perl
      root@xxxxxxxxxx:~# env && which perl
      SHELL=/bin/bash TERM=xterm USER=root MAIL=/var/mail/root PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PWD=/root LANG=en_US.UTF-8 SHLVL=1 HOME=/root LANGUAGE=en_US: LOGNAME=root LESSOPEN=| /usr/bin/lesspipe %s LESSCLOSE=/usr/bin/lesspipe %s %s _=/usr/bin/env /usr/bin/perl

        Unsurprisingly, the two environments you have listed there differ in the value of $HOME. Crypt::GPG uses this to find the keyrings, so if the keys aren't present on root's keyring, it won't stand a chance.

        It is likely that hippo's message above gives the right explanation, but I can tell you that I met a similar problem with a (DBI-Oracle) script that worked perfectly under my user and did not under the crontab (i.e. root user) because of environment variables being defined differently. I wasted almost a full day figuring out what the problem was, quite a poor performance if you think that I originally wrote the whole script in 2 or 3 hours.
Re: Script works when run with sudo, but not when run directly as root...?
by LanX (Bishop) on May 07, 2014 at 18:51 UTC
    That's the only error message you get?

    No complaint about not locating a module?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Correct. I don't get any other error messages.
        My thought:

        Check the environment variables of your account and of root created by the login shell.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1085362]
Approved by igelkott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2017-11-25 09:59 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (355 votes). Check out past polls.