LoginSignup
1
0

More than 1 year has passed since last update.

Unicorn の自動起動と etc/init.dの使い方

Last updated at Posted at 2020-04-30

railsをawsに置く作業をしていた時にUnicornの自動起動も試しておきたいと思いやったこと、学習したことを書きます。

Unicornの自動起動をする際に、etc/init.dにスクリプトを置くとのことで置いてみた。
これって、プログラミング言語で言うと何に当たるんだろう??と気になりつつも進めます。

まず、etc/init.d/にUnicornのfileを作成します。
作成した後には、chkconfig コマンドで自動起動をさせなきゃならない。

そもそも、initはlinuxが起動した時に呼ばれ実行する。その際、run levelが0~6まであり、2~5あたりのrun levelをchkconfig コマンドで変更する。ex)chkconfig on unicorn

変更すると、etc/rc0~6.dのfileにrun levelに応じたfileのシンボリックリンクが作成され、
実際に起動、読み込みするのは、etc/rc0.dだが、参照先としてetc/init.d/unicornが参照される。

そして、linixを起動すれば、etc/rcが動きunicornの中身も一緒に読み込まれて起動すると言う仕組みらしい。
以下は、etc/rc3.dをlsで見た際、S85unicornと言うfileができていることが確認できるかと。(シンボリックリンク付き)

[ec2-user@ip-00000 rc3.d]$ ls -la
合計 0
drwxr-xr-x  2 root root  63  4月 26 14:47 .
lrwxrwxrwx  1 root root  20  4月  7 01:51 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx  1 root root  17  4月  7 01:51 S10network -> ../init.d/network
lrwxrwxrwx  1 root root  17  4月 26 14:47 S85unicorn -> ../init.d/unicorn

Unicornの一部

start()
{
  if [ -e $PID ]; then # もしPIDがあれば
    echo "$NAME already started+${PID}"
    exit 1
  fi
  echo "start $NAME" # もしPIDがなければ
  su - ${USER} -c "cd ${ROOT_DIR} && ${CMD}"
}

これは、調べるとすんなり出てくる。

/etc/init.d/unicorn
#!/bin/sh

#chkconfig:   - 00 00
#description: unicorn shell

USER="ec2-user"
NAME="Unicorn"
ENV="production"

ROOT_DIR="/var/www/application"

PID="${ROOT_DIR}/tmp/pids/unicorn.pid"
CONF="${ROOT_DIR}/config/unicorn.rb"
OPTIONS=""
CMD="RAILS_SERVE_STATIC_FILES=1 unicorn_rails -c ${CONF} -E ${ENV} -D"

start()
{
  if [ -e $PID ]; then
    echo "$NAME already started+${PID}"
    exit 1
  fi
  echo "start $NAME"
  su - ${USER} -c "cd ${ROOT_DIR} && ${CMD}"
}

stop()
{
  if [ ! -e $PID ]; then
    echo "$NAME not started"
    exit 1
  fi
  echo "stop $NAME"
  kill -QUIT `cat ${PID}`
}

force_stop()
{
  if [ ! -e $PID ]; then
    echo "$NAME not started"
    exit 1
  fi
  echo "stop ${NAME}"
  echo ${PID}
  kill -INT `cat ${PID}`
}

reload()
{
  if [ ! -e $PID ]; then
    echo "$NAME not started"
    start
    exit 0
  fi
  echo "reload $NAME"
  ##kill -HUP `cat ${PID}`
  su - $USER -c "cd ${ROOT_DIR} && bundle exec unicorn_rails:reload"
  ## su $USER -lc "cd ${RAILS_ROOT}&& bundle exec rake unicorn_rails:reload"
}

restart()
{
  force_stop
  sleep 5
  start
}


case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  force-stop)
    force_stop
    ;;
  reload)
    reload
    ;;
  restart)
    restart
    ;;
  *)
    echo "Syntax Error: release [start|stop|force-stop|reload|restart]"
    ;;
esac

自動起動の際につまづいたことは、etc/environmentに環境変数入れていて手動では動いていたけども、
自動起動する際には動かなかったこと。
それは、Rails自体の環境変数がないためでcredentials.ymlなどに環境変数をきちんと設定してあげるといいと思います。
自分は、これで三日潰しました。

また、init自体はroot 権限で動かすみたいなので注意が必要かもしれません。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0