
基本的に実行する必要がある初期化スクリプトを作成したいと思います。
nvm use v0.11.12 && forever start /srv/index.js
ユーザーとしてwebconfig
。nvm
で宣言されたシェル関数で、~webconfig/.nvm/nvm.sh
'sを介して関数を含めます。source ~/.nvm/nvm.sh
webconfig
.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.
だからあなたができることは
ユーザー
~/.profile
またはで~/.bash_profile
機能を定義します。存在する~/.profile
場合は無視され~/.bash_profile
ます。また、これはbashに固有のものであるため、これを~/.bash_profile
使用することに注意してください。そこにないことを確認してください。.profile
~/.bash_profile
ソース
~/.nvm/nvm.sh
は~/.profile
。
答え2
@テデンここにいくつかの詳細が追加されたので、繰り返しません。繰り返しますが、問題の原因は、bash
ログインシェルを呼び出すときに自動的には発生せず、またはそれだけを見つける奇妙な動作である可能性が.bashrc
高いです。~/.bash_profile
~/.bash_login
~/.profile
.bashrc
一般的な方法は、これらの設定ファイルのいずれかにコードを追加することで、基本的に次のコードを持つことです~/.profile
。
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
修正する
以下のコメントによると、最良の解決策は[ -z "$PS1" ] && return
対応し.bashrc
、他のすべての内容は変更しないことです。
答え3
sudo -Hu webconfig bash -c '. ~/.nvm/nvm.sh && nvm'