Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2806

Railsでpumaをsystemdのserviceとして自動起動する手順

$
0
0

実行環境

  • Ubuntu Server 18.04
  • Rails 6.0.3
  • rbenv
  • nvm

systemdについて

  • systemdはユニットという単位でサービスなどを管理しています。
  • ユニットを作成するにはユニット設定ファイルを作成します。
  • ユニットにはservicesockettargetpathtimerなどの種類がありますが、今回はserviceについての説明です。
  • 例えばcronは、/lib/systemd/system/cron.serviceというファイルで設定されています。
/lib/systemd/system/cron.service
[Unit]
Description=Regular background program processing daemon
Documentation=man:cron(8)
After=remote-fs.target nss-user-lookup.target

[Service]
EnvironmentFile=-/etc/default/cron
ExecStart=/usr/sbin/cron -f $EXTRA_OPTS
IgnoreSIGPIPE=false
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • systemdの管理はsystemctlコマンドで行います。
  • $ systemctl status ○○.serviceでサービスの状況を確認できます。
$ systemctl status cron.service 
● cron.service - Regular background program processing daemon
     Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2020-06-14 13:29:11 JST; 2h 16min ago
       Docs: man:cron(8)
   Main PID: 761 (cron)
      Tasks: 1 (limit: 4444)
     Memory: 1.3M
     CGroup: /system.slice/cron.service
             └─761 /usr/sbin/cron -f
  • Active: active (runnig)で起動しているのを確認できます。
    • $ systemctl start ○○.serviceで起動
    • $ systemctl start ○○.serviceで停止
  • Loaded: loaded (/lib/systemd/system/cron.service; enabled;enableつまり自動起動が設定されていることが確認できます。
    • $ systemctl enable ○○.serviceで自動起動を設定
    • $ systemctl disable ○○.serviceで自動起動を解除

http://manpages.ubuntu.com/manpages/focal/en/man1/systemctl.1.html

○○.serviceファイルの保存場所

systemdのユニットを作成するには、ユニット設定ファイルを作成する必要があります。

  • /lib/systemd/system/○○.service
  • /etc/systemd/system/○○.service

のどちらかに保存するみたいです。
今回は、/etc/systemd/system/puma.serviceを作成します。

※他にも保存できる場所はあります。
http://manpages.ubuntu.com/manpages/focal/en/man5/systemd.unit.5.html

設定ファイルの内容

  • 設定ファイルには、Unit セクションService セクションInstall セクションというセクションがあります。
    • Unitセクションには基本的な設定を記述します。
    • Serviceセクションはserviceユニット用の設定を記述します。
/etc/systemd/system/puma.service
[Unit]
Description=Puma Rails Server
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/rails_root
ExecStart=/bin/bash -cl 'bin/rails s'
Restart=always
  • Unitセクション
    • Descriptionユニットの説明を記述します。
    • After指定したユニットがアクティブがされた後に開始します。今回はネットワークの実行後にのみ起動する設定です。

※正直network.target設定についてはよく理解できてません。
https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/

  • Serviceセクション
    • User実行するユーザー名を指定します。デフォルトはrootです。
    • WorkingDirectory実行するディレクトリを指定します。今回はRailsアプリのルートディレクトリを指定します。デフォルトはUserのホームディレクトリです。
    • ExecStart実行するコマンドを記述します。
    • Restartalwaysに設定すると異常終了した場合に再起動を試みます。デフォルトはnoです。

systemdは、ログインシェルとして起動しないのでユーザーの環境変数などは読み込まれません。
今回、ubuntuユーザーのホームディレクトリの~/.profileに設定されたrbenvnvmを読み込んで欲しいのでUserubuntu、コマンドを/bin/bash -lとして実行しています。
bash-lオプションをつけるとログインシェルと同じように環境変数が読み込まれます。

/home/ubuntu/.profile
#rbenvexport PATH="$HOME/.rbenv/bin:$PATH"eval"$(rbenv init -)"#nvmexport NVM_DIR="$HOME/.nvm"[-s"$NVM_DIR/nvm.sh"]&&\."$NVM_DIR/nvm.sh"# This loads nvm[-s"$NVM_DIR/bash_completion"]&&\."$NVM_DIR/bash_completion"# This loads nvm bash_completionexport RAILS_ENV=production

http://manpages.ubuntu.com/manpages/focal/en/man1/bash.1.html

※設定項目はもっとたくさんあります。
http://manpages.ubuntu.com/manpages/focal/en/man5/systemd.exec.5.html
http://manpages.ubuntu.com/manpages/focal/en/man5/systemd.unit.5.html
http://manpages.ubuntu.com/manpages/focal/en/man5/systemd.service.5.html

systemdの自動起動を設定

  1. $ systemctl daemon-reloadを実行して設定ファイルを読み込みます。
  2. $ systemctl start puma.serviceで起動します。
  3. $ systemctl status puma.serviceでちゃんと起動したか確認します。
  4. $ systemctl enable puma.serviceで自動起動を設定します。
  5. rebootして自動起動されたか確認します。

失敗した場合

  • systemdにはjournaldというログ記録機能があります。
  • journaldの確認にはjournalctlコマンドを利用します。
    • -uオプションでユニット名を指定できます。
    • -nオプションで末尾何行を表示するか指定できます。
$ journalctl -u puma.service -n 10
-- Logs begin at Tue 2020-06-09 03:08:04 UTC, end at Sun 2020-06-14 09:12:20 UTC. --
Jun 14 08:53:48 ip-10-0-0-177 systemd[1]: Started puma.
Jun 14 08:53:57 ip-10-0-0-177 bash[847]: => Booting Puma
Jun 14 08:53:57 ip-10-0-0-177 bash[847]: => Rails 6.0.3.1 application starting in production
Jun 14 08:53:57 ip-10-0-0-177 bash[847]: => Run `rails server --help`for more startup options
Jun 14 08:53:58 ip-10-0-0-177 bash[847]: Puma starting in single mode...
Jun 14 08:53:58 ip-10-0-0-177 bash[847]: * Version 4.3.5 (ruby 2.7.1-p83), codename: Mysterious Traveller
Jun 14 08:53:58 ip-10-0-0-177 bash[847]: * Min threads: 5, max threads: 5
Jun 14 08:53:58 ip-10-0-0-177 bash[847]: * Environment: production
Jun 14 08:53:58 ip-10-0-0-177 bash[847]: * Listening on unix:///home/ubuntu/rails_root/tmp/sockets/puma.sock
Jun 14 08:53:58 ip-10-0-0-177 bash[847]: Use Ctrl-C to stop

systemdについてのリンク


Viewing all articles
Browse latest Browse all 2806

Trending Articles