Systemd ( 1 )

October 04, 2017

Service のユニット定義ファイルを作る

simple 型の Service ユニットを作成

systemd の理解を深めるために、Service のユニット定義ファイルを作成してみる。
以下のスクリプトを実行するように、サービスを定義してみる。

$ cat /usr/local/bin/filesize 
#! /bin/sh

while :
do
	stat --format '%n: %s' $*
	sleep 60
done

定義ファイル

$ cat /etc/systemd/system/logsize.service
[Unit]
Description=Recording Log File Size

[Service]
Type=simple
ExecStart=/usr/local/bin/filesize /var/log/messages

ユニットを再読み込みし、サービスを起動。

# ユニットの再読み込み
$ sudo systemctl daemon-reload

# ユニットのステータスが loaded になっているか確認
$ sudo systemctl status logsize.service
● logsize.service - Recording Log File Size
   Loaded: loaded (/etc/systemd/system/logsize.service; static; vendor preset: disabled)
   Active: inactive (dead)

# ユニット起動
$ sudo systemctl start logsize.service

# ユニットのステータス確認
$ sudo systemctl status logsize.service
● logsize.service - Recording Log File Size
   Loaded: loaded (/etc/systemd/system/logsize.service; static; vendor preset: disabled)
   Active: active (running) since Wed 2017-10-04 01:23:34 UTC; 4s ago
 Main PID: 2243 (filesize)
   CGroup: /system.slice/logsize.service
           ├─2243 /bin/sh /usr/local/bin/filesize /var/log/messages
           └─2245 sleep 60

Oct 04 01:23:34 ip-172-31-14-250 systemd[1]: Started Recording Log File Size.
Oct 04 01:23:34 ip-172-31-14-250 systemd[1]: Starting Recording Log File Size...
Oct 04 01:23:34 ip-172-31-14-250 filesize[2243]: /var/log/messages: 884986
Oct 04 01:24:34 ip-172-31-14-250 filesize[2243]: /var/log/messages: 885055
Oct 04 01:25:34 ip-172-31-14-250 filesize[2243]: /var/log/messages: 885295
Oct 04 01:26:34 ip-172-31-14-250 filesize[2243]: /var/log/messages: 885364

起動したプログラムの出力がジャーナルとして保管されていることが確認できる。
ちなみに自動起動の設定をすると…

$ systemctl enable logsize.service
$ systemctl is-enabled logsize.service
static

-> static となってしまう。[Install] セクションで起動ターゲットに紐づける必要がある。

$ cat /etc/systemd/system/logsize.service
[Unit]
Description=Recording Log File Size

[Service]
Type=simple
ExecStart=/usr/local/bin/filesize /var/log/messages

[Install]
WantedBy=multi-suer.target

$ systemctl daemon-reload
$ systemctl enable logsize.service
Created symlink from /etc/systemd/system/multi-suer.target.wants/logsize.service to /etc/systemd/system/logsize.service.

なお、再起動の設定は以下のように行える。

$ cat /etc/systemd/system/logsize.service
[Unit]
Description=Recording Log File Size

[Service]
Type=simple
ExecStart=/usr/local/bin/filesize /var/log/messages
Restart=always

[Install]
WantedBy=multi-suer.target

kill してみる。

$ systemctl status logsize.service
● logsize.service - Recording Log File Size
   Loaded: loaded (/etc/systemd/system/logsize.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2017-10-14 10:29:12 UTC; 9min ago
 Main PID: 2301 (filesize)
   CGroup: /system.slice/logsize.service
           ├─2301 /bin/sh /usr/local/bin/filesize /var/log/messages
           └─2418 sleep 60

Oct 14 10:29:12 ip-172-31-14-250 filesize[2301]: /var/log/messages: 581322
Oct 14 10:30:12 ip-172-31-14-250 filesize[2301]: /var/log/messages: 581444
Oct 14 10:31:12 ip-172-31-14-250 filesize[2301]: /var/log/messages: 581513
Oct 14 10:32:12 ip-172-31-14-250 filesize[2301]: /var/log/messages: 581582
Oct 14 10:33:12 ip-172-31-14-250 filesize[2301]: /var/log/messages: 581651
Oct 14 10:34:12 ip-172-31-14-250 filesize[2301]: /var/log/messages: 581720
Oct 14 10:35:12 ip-172-31-14-250 filesize[2301]: /var/log/messages: 581895
Oct 14 10:36:12 ip-172-31-14-250 filesize[2301]: /var/log/messages: 581964
Oct 14 10:37:12 ip-172-31-14-250 filesize[2301]: /var/log/messages: 582033
Oct 14 10:38:12 ip-172-31-14-250 filesize[2301]: /var/log/messages: 582208

$ kill 2301

$ systemctl status logsize.service
● logsize.service - Recording Log File Size
   Loaded: loaded (/etc/systemd/system/logsize.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2017-10-14 10:39:00 UTC; 2s ago
 Main PID: 2430 (filesize)
   CGroup: /system.slice/logsize.service
           ├─2430 /bin/sh /usr/local/bin/filesize /var/log/messages
           └─2432 sleep 60

Oct 14 10:39:00 ip-172-31-14-250 systemd[1]: Started Recording Log File Size.
Oct 14 10:39:00 ip-172-31-14-250 systemd[1]: Starting Recording Log File Size...
Oct 14 10:39:00 ip-172-31-14-250 filesize[2430]: /var/log/messages: 582527

-> 起動してる。

Daemon 型の Service ユニットを作成

以下の filesize-daemon を作成。

$ cat /usr/local/bin/filesize-daemon
#! /usr/bin/bash

PIDFILE=/var/run/logsize.pid

/usr/local/bin/filesize	$* &

echo $! > $PIDFILE

disown %%

exit 0

ユニットファイルは以下。

$ cat /etc/systemd/system/logsize-daemon.service
[Unit]
Description=Recording Log File Size by Daemon

[Service]
Type=daemon
ExecStart=/usr/local/bin/filesize-daemon /var/log/messages
PIDFile=/var/run/logsize.pid

[Install]
WantedBy=multi-user.target

起動し、ステータスの確認。

$ systemctl daemon-reload
$ systemctl start logsize-daemon.service
$ systemctl status -l logsize-daemon.service
● logsize-daemon.service - Recording Log File Size by Daemon
   Loaded: loaded (/etc/systemd/system/logsize-daemon.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2017-10-14 10:56:00 UTC; 13s ago
 Main PID: 2569 (filesize)
   CGroup: /system.slice/logsize-daemon.service
           ├─2569 /bin/sh /usr/local/bin/filesize /var/log/messages
           └─2571 sleep 60

Oct 14 10:56:00 ip-172-31-14-250 systemd[1]: Started Recording Log File Size by Daemon.
Oct 14 10:56:00 ip-172-31-14-250 systemd[1]: Starting Recording Log File Size by Daemon...
Oct 14 10:56:00 ip-172-31-14-250 filesize-daemon[2568]: /var/log/messages: 587139

停止処理を定義する。

$ cat /etc/systemd/system/logsize-daemon.service
[Unit]
Description=Recording Log File Size by Daemon

[Service]
Type=daemon
ExecStart=/usr/local/bin/filesize-daemon /var/log/messages
ExecStop=/usr/local/bin/filesize-daemon-stop
PIDFile=/var/run/logsize.pid

[Install]
WantedBy=multi-user.target

$  cat /usr/local/bin/filesize-daemon-stop
#! /usr/bin/bash

PIDFILE=/var/run/logsize.pid

PID=`cat $PIDFILE`
rm -f $PIDFILE
kill $PID

これで、サービスが終了する前に filesize-daemon-stop を実行できる。


 © 2023, Dealing with Ambiguity