【问题标题】:can't start unicorn using init.d service – can't find bundle as sudo无法使用 init.d 服务启动独角兽 – 无法以 sudo 方式找到捆绑包
【发布时间】:2018-12-09 13:49:32
【问题描述】:

我正在尝试设置一个使用 capistrano 部署的 Rails 测试服务器。

我知道我的 capistrano 脚本可以正常工作,因为它使用相同的脚本毫无问题地部署到生产服务器。

在部署期间,应该启动独角兽,这样做

sudo service unicorn_appname start

被调用。

这会产生以下错误: Job for unicorn_appname.service failed because the control process exited with error code. See "systemctl status unicorn_appname.service" and "journalctl -xe" for details.

当我查看sudo journalctl -u unicorn_appname 时,

systemd[1]: Starting LSB: starts the unicorn web server...
su[3790]: Successful su for user by root
su[3790]: + ??? root:user
su[3790]: pam_unix(su:session): session opened for user user by (uid=0)
unicorn_appname[3787]: -su: bundle: command not found
systemd[1]: unicorn_appname.service: Control process exited, code=exited status=127
systemd[1]: Failed to start LSB: starts the unicorn web server.
systemd[1]: unicorn_appname.service: Unit entered failed state.
systemd[1]: unicorn_appname.service: Failed with result 'exit-code'.

/etc/init.d/unicorn_appname 存在以及何时在/etc/init.d

./unicorn_appname start 有效
sudo ./unicorn_appname start 但是给-su: bundle: command not found

但是 which bundlesudo which bundle 都显示相同的路径 (/home/user/.rbenv/shims/bundle)

如果可能的话,我不想更改脚本,因为它们在另一台服务器上工作。 所以我认为新服务器上有一些设置不同或缺失 - 但我不知道在哪里查看。

这是unicorn_appname的内容:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          unicorn
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the unicorn web server
# Description:       starts unicorn
### END INIT INFO

set -e

# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/user/apps/appname/current
PID_DIR=$APP_ROOT/tmp/pids
PID=$PID_DIR/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c /home/user/apps/appname/shared/config/unicorn.rb -E production"
AS_USER=user
set -u

OLD_PIN="$PID.oldbin"

sig () {
  test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
  test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}

workersig () {
  workerpid="$APP_ROOT/tmp/pids/unicorn.$2.pid"

  test -s "$workerpid" && kill -$1 `cat $workerpid`
}

run () {
  if [ "$(id -un)" = "$AS_USER" ]; then
    eval $1
  else
    su -c "$1" - $AS_USER
  fi
}

case "$1" in
start)
  sig 0 && echo >&2 "Already running" && exit 0
  run "$CMD"
  ;;
stop)
  sig QUIT && exit 0
  echo >&2 "Not running"
  ;;
force-stop)
  sig TERM && exit 0
  echo >&2 "Not running"
  ;;
kill_worker)
  workersig QUIT $2 && exit 0
  echo >&2 "Worker not running"
  ;;
restart|reload)
  sig USR2 && echo reloaded OK && exit 0
  echo >&2 "Couldn't reload, starting '$CMD' instead"
  run "$CMD"
  ;;
upgrade)
  if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
  then
    n=$TIMEOUT
    while test -s $OLD_PIN && test $n -ge 0
    do
      printf '.' && sleep 1 && n=$(( $n - 1 ))
    done
    echo

    if test $n -lt 0 && test -s $OLD_PIN
    then
      echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
      exit 1
    fi
    exit 0
  fi
  echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  run "$CMD"
  ;;
reopen-logs)
  sig USR1
  ;;
*)
  echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
  exit 1
  ;;
esac

您还需要更多信息吗?

编辑:

user 是我用来部署的用户。对于此系统上的root,没有安装任何内容。会不会是这个问题?

【问题讨论】:

  • 我以前从未使用任何 root 权限进行部署,所以我认为这不是问题(或者它也会在 prod 上标记)

标签: ruby-on-rails ruby unix capistrano unicorn


【解决方案1】:

我认为你的问题(可能)是:

cd $APP_ROOT; bundle exec unicorn -D -c /home/user/apps/appname/shared/config/unicorn.rb -E production"

假设这是您在测试服务器上使用的相同命令,您需要更改测试环境。

【讨论】:

  • 我知道你看到了什么,但这不是问题所在。我的环境设置正确。
【解决方案2】:

我想我找到了解决办法:

在 prod 服务器上,我有一个 /etc/profile.d/rbenv.sh,但在新服务器上丢失了。

这就是它的内容:

export RBENV_ROOT=/home/user/.rbenv
export PATH=$RBENV_ROOT/shims:$RBENV_ROOT/bin:$PATH

【讨论】:

猜你喜欢
  • 2012-06-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 2017-11-05
  • 2013-09-29
相关资源
最近更新 更多