3

I have the next script:

cd /home
touch $PF ; chown $NU.$NU $PF
su -p -s /bin/sh root -c "node"

When I run it, it raises the next error:

sh: node: command not found

But when I run it from the linux command line, it success and gives me the node command line.

What can be the reason for that?

7
  • different PATH? Try echo $PATH before executing the command. Commented Jul 6, 2014 at 8:29
  • show a ls -a /path/to/node Commented Jul 6, 2014 at 8:30
  • Question is offtopic here, try superuser.com to get an answer. Commented Jul 6, 2014 at 8:30
  • @mata su -p should preserve environment, including PATH, but I'm skeptical as well. Commented Jul 6, 2014 at 8:31
  • 1
    @mata good catch, it looked like a PATH problem. That's why I was skeptical. Glad it makes sense. Commented Jul 6, 2014 at 8:43

1 Answer 1

4

node is probably not in the root user's $PATH.

I checked the su documentation and noticed the following:

-m, -p, --preserve-environment
   Preserve the current environment, except for:

   $PATH
       reset according to the /etc/login.defs options ENV_PATH or ENV_SUPATH (see below);
[...]
   ENV_PATH (string)
       If set, it will be used to define the PATH environment variable when a regular user login. The value can be
       preceded by PATH=, or a colon separated list of paths (for example /bin:/usr/bin). The default value is
       PATH=/bin:/usr/bin.

   ENV_SUPATH (string)
       If set, it will be used to define the PATH environment variable when the superuser login. The value can be
       preceded by PATH=, or a colon separated list of paths (for example /sbin:/bin:/usr/sbin:/usr/bin). The default
       value is PATH=/sbin:/bin:/usr/sbin:/usr/bin.

So while you may have node in the current $PATH, it may not be in root's $PATH.

As some commenters have already mentioned, you may try giving an absolute $PATHto node: su -p -s /bin/sh root -c "/path/to/node"

If you can call node from your current user, try which node to determine the full path to the executable.

You may also try echoing your $PATH. su -p -s /bin/sh root -c 'echo $PATH'

Sign up to request clarification or add additional context in comments.

1 Comment

Good catch. That is a relative new change to man su occurring between coreutil 8.9 and the shuffle to util-linux 2.22.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.