Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System Shell Scripts Can't Find my Command in PATH

I put a custom command (shell script) in /usr/local/scripts/.

In order to see commands from /usr/local/scripts, I set the PATH using the following methods:

  1. sudo visudo

    Defaults  secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/scripts"
    
  2. sudo nano /etc/profile

    PATH="$PATH:/usr/local/scripts" export PATH

  3. sudo nano /etc/login.defs

    ENV_SUPATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/scripts ENV_PATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/usr/local/scripts

  4. sudo nano /root/.bashrc

    PATH="$PATH:/usr/local/scripts" export PATH

  5. sudo nano /etc/environment

    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/scripts"

And this works in most cases, but...

I have two script which, in their code, calls a script from /usr/local/scripts/, and it can't find my script!

The first is /etc/network/if-up.d/sendip, which is run as root when the networking stack is initialized.

And the second is /usr/local/scripts/notif-login which is run as root from pam by /etc/pam.d/sshd:

session optional pam_exec.so /usr/local/scripts/notif-login

If I run both script from my terminal shell, another user, with sudo, without sudo, after su, or login with root, it works properly. But when it is runner by the system (first when networking initialized, and the second via SSH) both failed to run scripts from /usr/local/scripts.

Is there another place where I have to set the path?

like image 547
santos82h Avatar asked Aug 07 '26 04:08

santos82h


1 Answers

Bash/sh will not read /etc/profile for "non-interactive shells", such as the shells from which the scripts you mention run. I'm not sure which distribution you're using, but you should just be able to add it to /etc/environment's PATH definition. Ob Ubuntu, for example:

Change:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

To:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/scripts"

Edit

This should work under Raspbian; it does under Debian. If it doesn't, you might need to just modify the path at the start of one of your scripts. None of the normal startup files will execute for a non-interactive session under Bash. Also try outputting $SHELL and make sure the script is running under the shell you think it is.

like image 83
Will Avatar answered Aug 09 '26 17:08

Will



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!