无法使用 init.d 服务启动 unicorn – 无法找到 bundle as sudo

can't start unicorn using init.d service – can't find bundle as sudo

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

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

在部署期间,应启动 unicorn,以执行此操作

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 - `cat $PID`
}

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

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

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

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

case "" 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  && 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: [=12=] <start|stop|restart|upgrade|force-stop|reopen-logs>"
  exit 1
  ;;
esac

您还需要更多信息吗?

编辑:

user 是我用来部署的用户。对于此系统上的 root,未安装任何内容。这会是问题所在吗?

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

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

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

我想我找到了解决方案:

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

内容如下:

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