0
1

【Linux覚書き】systemdとsystemctlでサービスを管理する

Last updated at Posted at 2024-08-25

初めに

前略、systemdとsystemctlの理解が怪しかったのでまとめました。

systemdとは

一言で言うと、バックグラウンドで動作するサービスやプロセスなどの起動状態を管理するデーモンのことです。

「バックグラウンドで動作するサービス」≒「デーモン」なので、systemdは「デーモンを管理するデーモン」のようなイメージですね。

systemdとその他のサービスの違い

systemdはすべてのプロセスの親プロセスにあたると言えます。

systemdはLinux起動時、一番最初に実行されるプロセスです。カーネルによって実行・管理されます(そのため、systemdのPIDは1です)。

対して、その他サービスはsystemdによって起動・管理されます。

すなわち、systemdとその他サービスは親子関係にあるということができます。

systemctlとは

systemdを操作するためのコマンドです。
すなわち、サービスを管理するコマンドということになります。

以下はApacheを起動するコマンドの例になります。(ubuntu)

$ systemctl start apache2

サービスのライフサイクル

ということで、systemdを用いて実際にサービスのライフサイクル(起動と停止)を管理してみましょう。

サマリ

  1. ユニットファイルを作成する
  2. systemctl daemon-reloadでsystemdにユニットファイルを認識させる
  3. systemctl startでサービスを起動する
  4. systemctl stopでサービスを停止する

1. ユニットファイルを作成する

ユニットファイルとは、systemdがサービスとして認識するための設定ファイルです。
/etc/systemd/system//usr/lib/systemd/system/などに配置されています。

以下はapacheのユニットファイルになります。

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=https://httpd.apache.org/docs/2.4/

[Service]
Type=forking
Environment=APACHE_STARTED_BY_SYSTEMD=true
ExecStart=/usr/sbin/apachectl start
ExecStop=/usr/sbin/apachectl graceful-stop
ExecReload=/usr/sbin/apachectl graceful
KillMode=mixed
PrivateTmp=true
Restart=on-abort
OOMPolicy=continue

[Install]
WantedBy=multi-user.target

ユニットファイルは様々な種類があり。サービスはその一つです。
その他にも、ソケットを管理するソケットユニットや、cronのように発火時間を管理するタイマーユニットなどがあります。

2. systemdにユニットファイルを認識させる

作成したユニットファイルをsystemdに管理対象として認識させます。systemctl daemon-reloadを用いて認識させましょう。

sudo systemctl daemon-reload

systemdが管理しているサービスの一覧は以下のコマンドで確認できます。表示されればOKです。

$ systemctl list-unit-files --type=service | grep apache2.service
apache2.service                              enabled         enabled

3. systemctl startでサービスを起動する

systemctl startコマンドでサービスを起動させます。

systemctl statusで起動していることを確認できます。
以下は起動中の状態です。(左上の●が○だと停止中)

$ sudo systemctl start apache2.service
$
$ systemctl status apache2.service
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
     Active: active (running) since Sun 2024-08-25 10:46:35 JST; 3min 13s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 3901 (apache2)
      Tasks: 55 (limit: 19034)
     Memory: 16.9M ()
     CGroup: /system.slice/apache2.service
             ├─3901 /usr/sbin/apache2 -k start
             ├─3903 /usr/sbin/apache2 -k start
             └─3904 /usr/sbin/apache2 -k start

4. systemctl stop

systemctl stopコマンドでサービスを停止させます。

$ sudo systemctl stop apache2.service
$
$ systemctl status apache2.service
○ apache2.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
     Active: inactive (dead) since Sun 2024-08-25 10:52:29 JST; 3s ago
   Duration: 5min 53.752s
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 4058 ExecStop=/usr/sbin/apachectl graceful-stop (code=exited, status=0/SUCCESS)
   Main PID: 3901 (code=exited, status=0/SUCCESS)

その他のsystemctlのコマンド

その他、systemctlでよく使うコマンドをいくつか紹介します。

再起動:systemctl restart

サービスを再起動するコマンドです。

$ sudo systemctl restart apache2

自動起動:systemctl enable

Linuxの起動時に、自動で起動するかどうかを設定するコマンドです。
enableで自動起動設定できます。

statusのLoaded行の2引数目が自動起動の設定状態を表しています。

$ sudo systemctl enable apache2
Synchronizing state of apache2.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable apache2
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /usr/lib/systemd/system/apache2.service.
$
$
$ systemctl status apache2
○ apache2.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
     Active: inactive (dead) since Sun 2024-08-25 10:10:11 JST; 7min ago
   Duration: 6.448s
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 3482 (code=exited, status=0/SUCCESS)

自動起動解除:systemctl disable

自動起動を解除するコマンドです。
system enableと比べると、Loaded行がdisabledになっていることが分かります。

$ sudo systemctl disable apache2
Synchronizing state of apache2.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install disable apache2
Removed "/etc/systemd/system/multi-user.target.wants/apache2.service".
$
$
$ systemctl status apache2
○ apache2.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/apache2.service; disabled; preset: enabled)
     Active: inactive (dead) since Sun 2024-08-25 10:10:11 JST; 1min 3s ago
   Duration: 6.448s
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 3482 (code=exited, status=0/SUCCESS)

終わりに

ずっとdemonだと思ってたんですが、daemonなんですね。。。
知見 of the year.

参考

https://qiita.com/miyuki_samitani/items/953140bc3c89f0fb606f
https://qiita.com/sinsengumi/items/24d726ec6c761fc75cc9

0
1
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
0
1