sudo(bash機能)でnvmを実行する

sudo(bash機能)でnvmを実行する

基本的に実行する必要がある初期化スクリプトを作成したいと思います。

nvm use v0.11.12 && forever start /srv/index.js

ユーザーとしてwebconfignvmで宣言されたシェル関数で、~webconfig/.nvm/nvm.sh'sを介して関数を含めます。source ~/.nvm/nvm.shwebconfig.bashrc

私は以下を試しました:

sudo -H -i -u webconfig nvm

echo "nvm" | sudo -H -i -u webconfig

しかし彼らは失敗しました

-bash:nvm:コマンドが見つかりません
-bash:line 1:nvm:コマンドが見つかりません

そのシェルで手動で実行してsudo -H -i -u webconfig入力すると機能します。nvm私は何が間違っていましたか?

ベストアンサー1

多くの場合、ここでは問題はさまざまな種類のシェルにあります。

  • たとえば、ターミナルエミュレータを開くと、gnome-terminal次のことが行われます。対話型、非ログインシェル。

  • コマンドラインからコンピュータにログインしたりsu username、同じコマンドをsudo -u username実行した場合対話型ログインシェル。

したがって、起動するシェルの種類に応じて、異なる起動ファイルセットを読み込みます。からman bash

   When  bash is invoked as an interactive login shell, or as a non-inter‐
   active shell with the --login option, it first reads and executes  com‐
   mands  from  the file /etc/profile, if that file exists.  After reading
   that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
   in  that order, and reads and executes commands from the first one that
   exists and is readable.  The --noprofile option may be  used  when  the
   shell is started to inhibit this behavior.

つまり、~/.bashrcログインシェルでは無視されます。-iオプションを使用しているため、sudoユーザーログインシェルの起動ファイル(from)を読み込んでいますman sudo

 -i, --login
             Run the shell specified by the target user's password data‐
             base entry as a login shell.  This means that login-specific
             resource files such as .profile or .login will be read by the
             shell. 

だからあなたができることは

  1. ユーザー~/.profileまたはで~/.bash_profile機能を定義します。存在する~/.profile場合は無視され~/.bash_profileます。また、これはbashに固有のものであるため、これを~/.bash_profile使用することに注意してください。そこにないことを確認してください。.profile~/.bash_profile

  2. ソース~/.nvm/nvm.sh~/.profile

おすすめ記事