Systemd | Linux
systemd とは linux のシステムを管理するデーモンです。
Systemd は Unit という単位でシステムを管理しています。
Unit は service, timer, device, target, socket, mount などのタイプがあります。
Unit ファイル
Unit ファイルを変更したときは sudo systemctl daemon-reload を実行します。
Unit タイプが serivce の場合は Unit ファイルとは別に service の設定ファイルが ~/.config/ などに配置される場合があります1。
Unit ファイルは以下に格納されます。
/lib/systemd/system/usr/lib/systemd/system/etc/systemd/system
/lib/systemd/system
インストール後の初期設定ファイルが配置されています。
ここに配置されているファイルは編集しません。
/usr/lib/systemd/system
インストール後の初期設定ファイルが配置されています。
ここに配置されているファイルは編集しません。
/etc/systemd/system
ユーザーが定義した Unit ファイルを配置します。
コマンド
systemctl
Unit の起動/停止/リスタート
// rsyslog
$ systemctl start|stop|restart rsyslog.service
// cron
$ systemctl start|stop|restart cron.service
// apache2
$ systemctl start|stop|restart apache2.service
Unit の状態
// rsyslog
$ systemctl status rsyslog.service
// cron
$ systemctl status cron.service
// apache2
$ systemctl status apache2.service
Unit 設定を確認
Unit 設定を確認する例として certbot.service を例に記載します。
$ systemctl cat certbot.service
# /lib/systemd/system/certbot.service # <======== ファイルパス
[Unit]
Description=Certbot
Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
Documentation=https://letsencrypt.readthedocs.io/en/latest/
[Service]
Type=oneshot
ExecStart=/usr/bin/certbot -q renew # <======== 実行コマンド
PrivateTmp=true
Unit 一覧
$ systemctl list-units
systemctl list-units は色々なオプションがあります。
$ systemctl list-units –all
これは、システムの現在の状態に関係なく、systemdがロードした、またはロードしようとしたユニットを表示します。実行後に非アクティブになったユニットや、systemdがロードしようとしたもののディスク上で見つからなかったユニットがあるかもしれません。他に、これらの結果をフィルタリングできるフラグがあります。たとえば、–state=フラグを使用して、表示したいLOAD、ACTIVE、またはSUB状態を示すことができます。systemctlが非アクティブなユニットでも表> 示するように、–allフラグを保持する必要があります。
systemctl list-units –all –state=inactive
他に一般的なフィルターとして、–type= があります。興味のあるタイプのユニットのみを表示するようにsystemctl に指示できます。たとえば、アクティブなサービスユニットのみを表示するには、次を使用できます。systemctl list-units –type=service
– https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units-ja
自動起動設定
Apache2 を例に記載します。 現状の自動起動を確認します。
$ sudo systemctl status apache2.service
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) # <===== この行を確認
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Thu 2022-12-29 05:28:43 UTC; 1h 48min ago
Main PID: 3136 (apache2)
Tasks: 55 (limit: 1134)
CGroup: /system.slice/apache2.service
├─3136 /usr/sbin/apache2 -k start
├─3139 /usr/sbin/apache2 -k start
└─3140 /usr/sbin/apache2 -k start
Dec 29 05:28:42 ip-10-1-2-54 systemd[1]: Starting The Apache HTTP Server...
Dec 29 05:28:43 ip-10-1-2-54 apachectl[3110]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.1.2.54. Set the 'ServerName' direct
Dec 29 05:28:43 ip-10-1-2-54 systemd[1]: Started The Apache HTTP Server.
以下のような設定になっています。
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
- 現状自動起動が有効:Loaded: loaded (/lib/systemd/system/apache2.service;
enabled; vendor preset: enabled) - ベンダーの初期設定は自動起動が有効:Loaded: loaded (/lib/systemd/system/apache2.service;
enabled;vendor preset: enabled)
自動起動を無効にしてみます。
$ sudo systemctl disable apache2.service
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable apache2
確認します。
sudo systemctl status apache2.service
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; disabled; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Thu 2022-12-29 05:28:43 UTC; 1h 53min ago
Main PID: 3136 (apache2)
Tasks: 55 (limit: 1134)
CGroup: /system.slice/apache2.service
├─3136 /usr/sbin/apache2 -k start
├─3139 /usr/sbin/apache2 -k start
└─3140 /usr/sbin/apache2 -k start
Dec 29 05:28:42 ip-10-1-2-54 systemd[1]: Starting The Apache HTTP Server...
Dec 29 05:28:43 ip-10-1-2-54 apachectl[3110]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.1.2.54. Set the 'ServerName' direct
Dec 29 05:28:43 ip-10-1-2-54 systemd[1]: Started The Apache HTTP Server.
- 自動起動が無効:Loaded: loaded (/lib/systemd/system/apache2.service;
disabled; vendor preset: enabled)
再度有効にしてみます。
$ sudo systemctl enable apache2.service
※ ちなみに無効化は $ sudo systemctl disable apache2.service です。
確認します。
$ sudo systemctl status apache2.service
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Thu 2022-12-29 05:28:43 UTC; 1h 56min ago
Main PID: 3136 (apache2)
Tasks: 55 (limit: 1134)
CGroup: /system.slice/apache2.service
├─3136 /usr/sbin/apache2 -k start
├─3139 /usr/sbin/apache2 -k start
└─3140 /usr/sbin/apache2 -k start
Dec 29 05:28:42 ip-10-1-2-54 systemd[1]: Starting The Apache HTTP Server...
Dec 29 05:28:43 ip-10-1-2-54 apachectl[3110]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.1.2.54. Set the 'ServerName' direct
Dec 29 05:28:43 ip-10-1-2-54 systemd[1]: Started The Apache HTTP Server.
- 自動起動が有効:Loaded: loaded (/lib/systemd/system/apache2.service;
enabled; vendor preset: enabled)
timer
timer一覧
$ systemctl list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
水 2023-01-04 11:09:00 JST 2min 28s left 水 2023-01-04 10:39:10 JST 27min ago phpsessionclean.timer phpsessionclean.service
水 2023-01-04 19:17:20 JST 8h left 水 2023-01-04 02:52:17 JST 8h ago certbot.timer certbot.service
水 2023-01-04 19:45:01 JST 8h left 火 2023-01-03 19:45:01 JST 15h ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.serv
木 2023-01-05 02:32:55 JST 15h left 水 2023-01-04 06:39:10 JST 4h 27min ago apt-daily.timer apt-daily.service
木 2023-01-05 06:38:09 JST 19h left 水 2023-01-04 06:41:10 JST 4h 25min ago apt-daily-upgrade.timer apt-daily-upgrade.service
5 timers listed.
Pass --all to see loaded but inactive timers, too.
lines 1-9/9 (END)
timerとservice
この timer で実行するユニットを指定する(本例ではコメントアウト)。 指定がなければ timer と同名の service を実行する。
ref. https://vlike-vlife.netlify.app/posts/systemd_scheduling
certbot.timerの例(Unitが省略されているので同名のサービスを実行する)
systemctl cat certbot.timer
# /lib/systemd/system/certbot.timer
[Unit]
Description=Run certbot twice daily
[Timer]
OnCalendar=*-*-* 00,12:00:00
RandomizedDelaySec=43200
Persistent=true
[Install]
WantedBy=timers.target
journalctl
systemdのログを表示するコマンドがjournalctlコマンドです。
apache2のsystemdのログを表示します。
$ journalctl -u apache2.service
詳細なログを表示します。
$ journalctl -ex -u apache2.service
u:Unit(サービス)を指定e:最新のログを表示x:詳細なログを表示
journalファイルの削除
Linux環境設定/ジャーナルファイルを削除したい場合・journalctl
-
Unit ファイルを変更したときは
sudo systemctl daemon-reloadが必要です。 service の設定ファイルを変更した場合はsudo systemctl reload .serviceで良いです。 ↩