仕事で3時間くらいハマったので書く。
結論
service
ではなく、systemd
を使う。
参考情報
nginxの場合
$ sudo systemctl list-unit-files | grep nginx sudo: unable to resolve host ip-10-100-3-162 nginx.service enabled
設定ファイルの実体は以下。
$ pwd /etc/systemd/system/multi-user.target.wants
設定ファイルの内容
# Stop dance for nginx # ======================= # # ExecStop sends SIGSTOP (graceful stop) to the nginx process. # If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control # and sends SIGTERM (fast shutdown) to the main process. # After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends # SIGKILL to all the remaining processes in the process group (KillMode=mixed). # # nginx signals reference doc: # http://nginx.org/en/docs/control.html # [Unit] Description=A high performance web server and a reverse proxy server After=network.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;' ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;' ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid TimeoutStopSec=5 KillMode=mixed [Install] WantedBy=multi-user.target
今回の環境
- Ubuntu 16.04
- Rails5
- Nginx
- Unicorn
ダメな設定方法
- playbook.yml
- name: start service unicorn service: name=unicorn state=started enabled=yes
実行結果は以下の通り。 エラーとなる。
TASK [start service unicorn] *************************************************** [WARNING]: The service (unicorn) is actually an init script but the system is m fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Unable ervice is not a native service, redirecting to systemd-sysv-install\nExecuting / le unicorn\ninsserv: warning: script 'unicorn' missing LSB tags and overrides\nu art contains no runlevels, aborting.\n"}
良い設定方法
- unicorn.service
[Unit] Description=Production Unicorn Server [Service] WorkingDirectory=/home/ubuntu/hoge Environment=RAILS_ENV=production SyslogIdentifier=unicorn PIDFile=/home/ubuntu/hoge/tmp/pids/unicorn.pid ExecStart=/home/ubuntu/.rbenv/shims/bundle exec "unicorn_rails -c /home/ubuntu/hoge/config/unicorn.rb -E production" ExecStop=/bin/kill -QUIT $MAINPID ExecReload=/bin/kill -USR2 $MAINPID [Install] WantedBy=multi-user.target
- playbook.yml
- name: copy unicorn.service file copy: src=templates/{{ server_env }}/unicorn/unicorn.service dest=/lib/systemd/system/ - name: start service unicorn systemd: name: unicorn state: started daemon_reload: yes enabled: yes
これで無事に自動起動できるようになります。 今日はとてもいい仕事をした!