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

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.

--MidLifeXis

Replies are listed 'Best First'.
Re^2: Script works when run with sudo, but not when run directly as root...?
by bmalnad (Initiate) on May 07, 2014 at 20:42 UTC
    I can't find anything that makes any sense. dlamb@xxxxxxxxxx:~$ env && which perl
    TERM=xterm SHELL=/bin/bash SSH_CLIENT=172.20.202.137 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=172.20.202.137 38907 172.21.202.5 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.