systemdで任意のプログラムをサービス化する
以下のような HELLO, WORLD!
を出力するだけのプログラムが /usr/local/bin/hello
にあるとする
hello.sh#!/bin/sh
while :; do
echo "HELLO, WORLD!"
sleep 1
done
次の内容でサービスユニットファイルを /etc/systemd/system/hello.service
に作成する
/etc/systemd/system/hello.service[Unit]
Description=hello service
After=network.target auditd.service
[Service]
ExecStart=/usr/local/bin/hello
Restart=always
RestartSec=1
StartLimitBurst=0
[Install]
WantedBy=multi-user.target
次のコマンドを実行してサービスを有効化する
shsudo systemctl enable hello
次のコマンドを実行してサービスを起動する
shsudo systemctl start hello
次のコマンドを実行するとログを表示できる
shsudo journalctl -f -u hello