1.起動スクリプト/停止スクリプトを作成する。
startHoge.sh
# !/bin/bash
echo start-hoge >> /tmp/hoge.log
stopHoge.sh
# !/bin/bash
echo stop-hoge >> /tmp/hoge.log
ここではサンプルのため「start-hoge」「stop-hoge」がファイルに出力されるものを作成する。
2.実行権限を付与
chmod 777 startHoge.sh
chmod 777 stopHoge.sh
3./etc/systemd/system/xxx.serviceに定義を設定
[Unit]
Description = hoge daemon
After=syslog.target network.target
[Service]
ExecStart = /home/startHoge.sh
ExecStop = /home/stopHoge.sh
Restart = no
Type = simple
User = user
[Install]
WantedBy = multi-user.target
ExecStartに起動スクリプト、ExecStopに停止スクリプトを設定する。
4.確認1
systemctl list-unit-files --type=service | grep xxx.service を実行する。
以下が出力されればサービス登録できている。
xxx.service disabled
5.確認2
systemctl start xxx.service
systemctl status xxx.service
systemctl stop xxx.service