実行環境
- Ubuntu Server 18.04
- Rails 6.0.3
- rbenv
- nvm
systemd
について
systemd
はユニットという単位でサービスなどを管理しています。- ユニットを作成するにはユニット設定ファイルを作成します。
- ユニットには
service
、socket
、target
、path
、timer
などの種類がありますが、今回はservice
についての説明です。 - 例えば
cron
は、/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
ユニット用の設定を記述します。
[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
実行するコマンドを記述します。Restart
always
に設定すると異常終了した場合に再起動を試みます。デフォルトはno
です。
systemd
は、ログインシェルとして起動しないのでユーザーの環境変数などは読み込まれません。
今回、ubuntu
ユーザーのホームディレクトリの~/.profile
に設定されたrbenv
、nvm
を読み込んで欲しいのでUser
をubuntu
、コマンドを/bin/bash -l
として実行しています。bash
に-l
オプションをつけるとログインシェルと同じように環境変数が読み込まれます。
#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
の自動起動を設定
$ systemctl daemon-reload
を実行して設定ファイルを読み込みます。$ systemctl start puma.service
で起動します。$ systemctl status puma.service
でちゃんと起動したか確認します。$ systemctl enable puma.service
で自動起動を設定します。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
についてのリンク
はじめからこれをみればよかった
読みやすい
- https://linuc.org/textbooks/admin/
- https://wiki.archlinux.jp/index.php/Systemd
- https://www.debian.org/doc/manuals/debian-handbook/unix-services.ja.html
- https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/8/html/configuring_basic_system_settings/managing-services-with-systemd_configuring-basic-system-settings
読めない
Manpage
- http://manpages.ubuntu.com/manpages/focal/en/man1/systemd.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
- http://manpages.ubuntu.com/manpages/focal/en/man1/systemctl.1.html
- http://manpages.ubuntu.com/manpages/focal/en/man1/journalctl.1.html