Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux/ Why su command is working from CMD but not from script?

Tags:

linux

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?

like image 652
Or Smith Avatar asked May 18 '26 11:05

Or Smith


1 Answers

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'

like image 112
Jon Surrell Avatar answered May 21 '26 04:05

Jon Surrell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!