LoginSignup
0
2

More than 5 years have passed since last update.

起動スクリプト/停止スクリプトをサービス化

Posted at

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

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2