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:

  • sudo visudo

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

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

  • 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

  • sudo nano /root/.bashrc

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

  • 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?


    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.

    链接地址: http://www.djcxy.com/p/56850.html

    上一篇: 我无法设置cron作业

    下一篇: 系统Shell脚本无法在PATH中找到我的命令