/etc/init.d/独角兽

/etc/init.d/独角兽

我在同一个 DigitalOcean Ubuntu 机器上运行我的暂存环境和生产环境。我已让所有工作正常进行,以便每个环境都可以部署到其自己的目录中,使用单独的 pid 文件运行单独的 unicorn 进程,并且 nginx 配置了单独的启用站点的文件,以将每个 unicorn 实例与其适当的环境关联起来。

我的部署过程正确地(重新)启动了匹配环境的 unicorn。问题是当我需要重新启动盒子本身时,我的盒子/etc/init.d/unicorn仅配置为启动生产环境。我不确定我需要做什么才能使生产环境和暂存环境在启动时启动,而不是创建辅助/etc/init.d/unicorn-staging文件/etc/default/unicorn-staging

以下是我现有的文件,供参考。

/etc/init.d/独角兽

#!/bin/sh
### BEGIN INIT INFO
# Provides:          unicorn
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: unicorn initscript
# Description:       unicorn
### END INIT INFO

set -e
NAME=unicorn
DESC="Unicorn web server"

. /lib/lsb/init-functions

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

PID=${PID-/run/unicorn.pid}

run_by_init() {
    ([ "${previous-}" ] && [ "${runlevel-}" ]) || [ "${runlevel-}" = S ]
}

exit_with_message() {
  if ! run_by_init; then
    log_action_msg "$1 Not starting."
  fi
  exit 0
}

check_config() {
  if [ $CONFIGURED != "yes" ]; then
    exit_with_message "Unicorn is not configured (see /etc/default/unicorn)."
  fi
}

check_app_root() {
  if ! [ -d $APP_ROOT ]; then
    exit_with_message "Application directory $APP_ROOT is not exist."
  fi
}

set -u

case "$1" in
  start)
        check_config
        check_app_root

        log_daemon_msg "Starting $DESC" $NAME || true
        if start-stop-daemon --start --quiet --oknodo --pidfile $PID --exec $DAEMON -- $UNICORN_OPTS; then
          log_end_msg 0 || true
        else
          log_end_msg 1 || true
        fi
          ;;
  stop)
        log_daemon_msg "Stopping $DESC" $NAME || true
        if start-stop-daemon --stop --signal QUIT --quiet --oknodo --pidfile $PID; then
          log_end_msg 0 || true
        else
          log_end_msg 1 || true
        fi
        ;;
  force-stop)
        log_daemon_msg "Forcing stop of $DESC" $NAME || true
        if start-stop-daemon --stop --quiet --oknodo --pidfile $PID; then
          log_end_msg 0 || true
        else
          log_end_msg 1 || true
        fi
        ;;
  restart|force-reload)
        log_daemon_msg "Restarting $DESC" $NAME || true
        start-stop-daemon --stop --quiet --oknodo --pidfile $PID
        sleep 1
        if start-stop-daemon --start --quiet --oknodo --pidfile $PID --exec $DAEMON -- $UNICORN_OPTS; then
          log_end_msg 0 || true
        else
          log_end_msg 1 || true
        fi
        ;;
  reload)
        log_daemon_msg "Reloading $DESC" $NAME || true
        if start-stop-daemon --stop --signal HUP --quiet --oknodo --pidfile $PID; then
          log_end_msg 0 || true
        else
          log_end_msg 1 || true
        fi
        ;;
  reopen-logs)
        log_daemon_msg "Relopening log files of $DESC" $NAME || true
        if start-stop-daemon --stop --signal USR1 --quiet --oknodo --pidfile $PID; then
          log_end_msg 0 || true
        else
          log_end_msg 1 || true
        fi
        ;;
  status)
        status_of_proc -p $PID $DAEMON $NAME && exit 0 || exit $?
        ;;
  *)
        log_action_msg "Usage: $0 <start|stop|restart|force-reload|reload|force-stop|reopen-logs|status>" || true
        exit 1
        ;;
esac

/etc/default/独角兽

# Change paramentres below to appropriate values and set CONFIGURED to yes.
CONFIGURED=yes

# Default timeout until child process is killed during server upgrade,
# it has *no* relation to option "timeout" in server's config.rb.
TIMEOUT=60

# Path to your web application, sh'ld be also set in server's config.rb,
# option "working_directory". Rack's config.ru is located here.
APP_ROOT=/var/www/example.com/current

# Server's config.rb, it's not a rack's config.ru
CONFIG_RB=/var/www/example.com/current/config/unicorn.rb

# Where to store PID, sh'ld be also set in server's config.rb, option "pid".
PID=/var/www/coderfit.com/shared/tmp/pids/unicorn.pid
UNICORN_OPTS="-D -c $CONFIG_RB -E production"

PATH=/usr/local/rvm/rubies/ruby-2.2.0/bin:/var/www/example.com/shared/bundle/ruby/2.2.0/bin:/usr/local/sbin:/usr/bin:/bin:/sbin:$
export GEM_HOME=/usr/local/rvm/gems/ruby-2.2.0@example
export GEM_PATH=/var/www/example.com/shared/bundle/ruby/2.2.0:/usr/local/rvm/gems/ruby-2.2.0@example:/usr/local/rvm/gems/ruby-2.2.0@global
DAEMON=/var/www/example.com/shared/bundle/ruby/2.2.0/bin/unicorn

答案1

您可以复制/etc/init.d/unicorn/etc/init.d/unicorn_app2

编辑unicorn_app2文件:

  • 变成/etc/default/unicorn/etc/default/unicorn_app2
  • 变成/run/unicorn.pid/run/unicorn_app2.pid

复制/etc/default/unicorn/etc/default/unicorn_app2

编辑/etc/default/unicorn_app2以设置您的第二个应用程序

运行新服务器service unicorn_app2 start,还为第二个应用程序设置 nginx/apache(使用新的 pid 文件)

相关内容