Sudo는 다른 사용자로 명령을 실행하지 않습니다.

Sudo는 다른 사용자로 명령을 실행하지 않습니다.

서버가 부팅될 때 유니콘 서버가 시작되도록 하려고 합니다. 우분투 사용자로 로그인하고 실행하면 작동하는 쉘 스크립트를 만들었습니다.

/etc/init.d/unicorn start

쉘 스크립트

#!/bin/sh

case "$1" in
start)
    cd /home/ubuntu/projects/asbest/current/
    unicorn_rails -c /home/ubuntu/projects/asbest/current/config/unicorn.rb -D -E production

;;
stop)
    if ps aux | awk '{print $2 }' | grep `cat ~/projects/asbest/current/tmp/pids/unicorn.pid`> /dev/null; then kill `cat ~/projects/asbest/current/tmp/pids/uni$
;;
restart)
    $0 stop
    $0 start
;;
esac

서버를 재부팅했을 때 유니콘 서버가 소켓을 수신하지 않는 것을 발견했습니다. 우분투 사용자로서 코드를 성공적으로 실행했기 때문에 항상 sudo를 통해 우분투 사용자를 사용할 수 있도록 스크립트를 수정했습니다.

#!/bin/sh

case "$1" in
start)
    cd /home/ubuntu/projects/asbest/current/
    sudo -u ubuntu unicorn_rails -c /home/ubuntu/projects/asbest/current/config/unicorn.rb -D -E production
;;
stop)
    if ps aux | awk '{print $2 }' | grep `cat ~/projects/asbest/current/tmp/pids/unicorn.pid`> /dev/null; then sudo -u ubuntu kill `cat ~/projects/asbest/current/tmp/pids/uni$
;;
restart)
    $0 stop
    $0 start
;;
esac

유니콘을 재부팅한 후에도 여전히 시작되지 않으므로 명령줄에서 스크립트를 실행해 보았습니다. 이제 다음과 같은 오류가 발생합니다.

sudo: unicorn_rails: command not found

나는 이 문제의 원인이 무엇인지 찾아보았지만 Linux에 대한 나의 제한된 이해를 활용한 것 같습니다. 내가 이해할 수 있는 것은 sudo가 명령을 실행하기 위해 ubuntu 사용자를 사용해야 하지만 여전히 Ruby 또는 unicorn을 실행하도록 구성되지 않은 루트 사용자의 환경을 사용한다는 것입니다. 누구든지 이것에 대한 경험이 있습니까?

답변1

다음과 같이 전역 변수를 사용하십시오 UNICORN_*.

UNICORN_HOME=/the/path
UNICORN_RAIL=${UNICORN_HOME}/unicorn_rail
UNICORN_CONFIG=${UNICORN_HOME}/config/unicorn.rb
UNICORN_PID=${UNICORN_HOME}/tmp/pids/unicorn.pid
UNICORN_USER=ubuntu

sudo -u ${UNICORN_USER} ${UNICORN_RAIL} -c $UNICORN_CONFIG -D -E production

또 다른 좋은 방법은 다음에서 전역 변수를 추출하는 것입니다 /etc/default/unicorn.

UNICORN_HOME=/the/path
UNICORN_RAIL=${UNICORN_HOME}/unicorn_rail
UNICORN_CONFIG=${UNICORN_HOME}/config/unicorn.rb
UNICORN_PID=${UNICORN_HOME}/tmp/pids/unicorn.pid
UNICORN_USER=ubuntu

초기화 스크립트에서 다음을 통해 모든 변수를 변경합니다.

if [ -f /etc/default/unicorn ]; then
    . /etc/default/unicorn
fi

답변2

를 사용하는 대신 sudo을 사용 su [username]하고 명령을 실행해 보십시오.

답변3

다음 경로를 지정해야 합니다 unicorn_rails.

UNICORN_HOME=/home/ubuntu/projects/asbest/current
cd $UNICORN_HOME
sudo -u ubuntu $UNICORN_HOME/unicorn_rails -c $UNICORN_HOME/config/unicorn.rb -D -E production

sudo를 적절하게 구성했습니까?

관련 정보