다른 사용자로 실행되는 명령이 많은 bash 스크립트에서 작동하지 않는 것 같은 명령 클래스를 발견했습니다.
Rails 저장소의 루트로서:
> su -c 'whoami' at
at
> su -c 'rake tmp:clear' at
rake aborted!
LoadError: cannot load such file -- bundler/setup
> login at
> rake tmp:clear
(Success)
rails [something]
등과 같은 다양한 Rails 지향 명령의 경우 rake [something]
명령은 실제로 at로 로그인한 경우에만 작동합니다.
여기서 무슨 일이 일어나고 있는 걸까요?
답변1
이는 환경 문제인 것 같습니다 $PATH
. 이러한 명령을 실행하면 su
다른 사용자 환경의 컨텍스트에서 명령이 실행됩니다. 확인하려면 a를 실행해 보고 사용자로 로그인했을 때의 결과와 비교해 su -c 'echo $PATH' at
보세요 .$PATH
at
Bash와 같은 쉘에는 일반적으로 구성 파일을 가져오는 두 가지 방법이 있습니다. Bash에는 2개가 있으며 $HOME/.bashrc
일반적 $HOME/.bash_profile
으로 이것이 실행되는 것입니다. 구성을 얻는 이 두 가지 방법은 대화형 및 비대화형이라고 하며 man bash
Bash 페이지의 호출 섹션에서 논의됩니다.
INVOCATION
A login shell is one whose first character of argument zero is a -, or
one started with the --login option.
An interactive shell is one started without non-option arguments and
without the -c option whose standard input and error are both connected to
terminals (as determined by isatty(3)), or one started with the -i option. PS1
is set and $- includes i if bash is interactive, allowing a shell script or a
startup file to test this state.
The following paragraphs describe how bash executes its startup files.
If any of the files exist but cannot be read, bash reports an error. Tildes are
expanded in file names as described below under Tilde Expansion in the
EXPANSION section.
When bash is invoked as an interactive login shell, or as a
non-interactive shell with the --login option, it first reads and executes
commands from the file /etc/profile, if that file exists. After read‐ ing 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.
When a login shell exits, bash reads and executes commands from the files
~/.bash_logout and /etc/bash.bash_logout, if the files exists.
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists. This may be
inhibited by using the --norc option. The --rcfile file option will force bash
to read and execute commands from file instead of ~/.bashrc.
When bash is started non-interactively, to run a shell script, for
example, it looks for the variable BASH_ENV in the environment, expands its
value if it appears there, and uses the expanded value as the name of a file to
read and execute. Bash behaves as if the following command were executed: if [
-n "$BASH_ENV" ]; then . "$BASH_ENV"; fi but the value of the PATH variable is
not used to search for the file name.