systemdによる自動起動@CentOS7

Linux

CentOS6 までは、サービス起動デーモンとして Upstart を使ってきましたが、
CentOS7 からは systemd が使われるようになったみたいです。
なので、少し戸惑いつつも少しづつ慣れようと思います。

各種サービスは Unit という単位で管理されるようです。

■ 動かすサービスを作る
まず、サンプルとして、起動し続けるシェルを用意しておきます。

# vim /usr/local/sbin/detect_error.sh
#!/bin/sh

while [ 1 ]; do
  echo "checking error. (PID=$(echo $$), DATE=$(date))"
  sleep 300
done

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

# vim /usr/local/sbin/detect_error.sh
#!/bin/sh

while [ 1 ]; do
  echo "checking error. (PID=$(echo $$), DATE=$(date))"
  sleep 300
done

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

# vim /etc/systemd/system/detect_error.service
[Unit]
Description=detect_error_at_syslog
After=network.service

[Service]
Type=simple
Restart=on-success
ExecStart=/usr/local/sbin/detect_error.sh

[Install]
WantedBy=multi-user.target

■ systemctlをリロード

# systemctl daemon-reload

■ 起動

# systemctl start detect_error

■ 自動起動の設定

# systemctl enable detect_error

これでひとまづ自動起動できる状態になりました。
シェルの中身を書き換えて監視系の処理を入れていきたいと思います。

タイトルとURLをコピーしました