Systemd で簡単なサービスを定義してみる
概要
いろいろな Linux ディストリビューションで採用されてきている Systemd を理解するために、 hello world
を一秒ごとに標準出力へ吐き出す簡単なサービスを定義し、 journalctl
でログを確認してみた。
内容は↑の記事とほぼ同様になっているが、 下記では標準出力へそのまま hello world
を出力しているので、 jounarlctl
でログを確認できる点で異なる。
Systemd のバージョン確認
使ったディストリビューションは Ubuntu 16.04 で、Systemd の バージョンは 229。
$ lsb_release -d
Description: Ubuntu 16.04.3 LTS
$ systemd --version
systemd 229
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN
/usr/local/bin/hello-daemon.sh
1秒ごとに標準出力に hello world
を出力するプログラム /usr/local/bin/hello-daemon.sh
を用意する。
↓で実行権限をつける。
/etc/systemd/system/hello.service
hello.service
を定義するために /etc/systemd/system/hello.service
を用意する。
[Unit]
Description = hello daemon
[Service]
ExecStart = /usr/local/bin/hello-daemon.sh
Restart = always
Type = simple
[Install]
WantedBy = multi-user.target
hello.service の自動起動を有効にする
↓で hello.service
の自動起動を有効化できる。
$ sudo systemctl enable hello.service
Created symlink from /etc/systemd/system/multi-user.target.wants/hello.service to /etc/systemd/system/hello.service.
↓で状態の確認。
$ sudo systemctl status hello.service
● hello.service - hello daemon
Loaded: loaded (/etc/systemd/system/hello.service; enabled; vendor preset: enabled)
Active: inactive (dead)
hello.service の開始
↓で hello.service
を開始できる。
↓状態の確認。
$ sudo systemctl status hello.service
● hello.service - hello daemon
Loaded: loaded (/etc/systemd/system/hello.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2017-12-08 12:28:32 UTC; 4s ago
Main PID: 21220 (hello-daemon.sh)
CGroup: /system.slice/hello.service
├─21220 /bin/bash /usr/local/bin/hello-daemon.sh
└─21235 sleep 1
Dec 08 12:28:32 hostname systemd[1]: Started hello daemon.
Dec 08 12:28:32 hostname hello-daemon.sh[21220]: hello world
Dec 08 12:28:33 hostname hello-daemon.sh[21220]: hello world
Dec 08 12:28:34 hostname hello-daemon.sh[21220]: hello world
Dec 08 12:28:35 hostname hello-daemon.sh[21220]: hello world
hello.service のプロセスをkillしてみる
hello-daemon.sh
の PID が 21220
だったので、↓で kill
してみる。
systemctl status
で確認すると新しいプロセスが PID 21442
で立ち上がったことがわかる。
$ sudo systemctl status hello.service
● hello.service - hello daemon
Loaded: loaded (/etc/systemd/system/hello.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2017-12-08 12:31:33 UTC; 3s ago
Main PID: 21442 (hello-daemon.sh)
CGroup: /system.slice/hello.service
├─21442 /bin/bash /usr/local/bin/hello-daemon.sh
└─21446 sleep 1
Dec 08 12:31:33 hostname systemd[1]: Started hello daemon.
Dec 08 12:31:33 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:31:34 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:31:35 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:31:36 hostname hello-daemon.sh[21442]: hello world
journalctl でログの確認
hello.service
のログは journalctl
を使って↓で確認できる。 (-n
オプションで5行に制限している。)
$ sudo journalctl -u hello.service -n 5
-- Logs begin at Wed 2017-12-06 05:21:38 UTC, end at Fri 2017-12-08 12:34:45 UTC. --
Dec 08 12:34:41 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:34:42 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:34:43 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:34:44 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:34:45 hostname hello-daemon.sh[21442]: hello world
hello.service の停止
↓で hello.servce
を停止できる。
↓プロセスが止まったことを確認できた。
$ sudo systemctl status hello.service
● hello.service - hello daemon
Loaded: loaded (/etc/systemd/system/hello.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Fri 2017-12-08 12:39:17 UTC; 6s ago
Process: 21442 ExecStart=/usr/local/bin/hello-daemon.sh (code=killed, signal=TERM)
Main PID: 21442 (code=killed, signal=TERM)
Dec 08 12:39:09 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:39:10 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:39:11 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:39:12 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:39:13 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:39:14 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:39:15 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:39:16 hostname hello-daemon.sh[21442]: hello world
Dec 08 12:39:17 hostname systemd[1]: Stopping hello daemon...
Dec 08 12:39:17 hostname systemd[1]: Stopped hello daemon.
あとがき
ログの処理は journald で一括して設定できるので楽になった? かも。 (journald の設定ファイルは /etc/systemd/journald.conf
にある。)