zuntan02のはてなブログ

備忘録的なものです。時々職場の技術者ブログにも転記してますが、メインはこちらで。

Amazonlinux2でsupervisorを動かすまで

■supervisorとは?

https://www.task-notes.com/entry/20170311/1489224418

Supervisor とは Python 製のプロセス管理ツールで、プログラムなどを簡単にデーモン化することができます。

https://qiita.com/yushin/items/15f4f90c5663710dbd56

プロセス管理/デーモン化のツール。 コンフィグちょっと書くだけで簡単にデーモンプロセスの生成/管理が可能。 本家のドキュメントはこちら http://supervisord.org/

【検証環境構築】

Amazonlinux2にsupervisorを入れてみる 【参照】 https://qiita.com/abouch/items/3f4b422c7d9048936629 http://supervisord.org/installing.html ←公式

pipのインストール

sudo easy_install pip

Installed /usr/lib/python2.7/site-packages/pip-19.1.1-py2.7.egg

supervisorのインストール

pip install supervisor

Successfully installed meld3-1.0.2 supervisor-4.0.3

バージョン確認
supervisord -v

4.0.3

サービス起動設定を行う

pipでのインストールではデフォルトではsupervisord自体のサービス管理は含まれません。動かすために以下の設定を行います。

デフォルトコンフィグファイル生成

echo_supervisord_conf コマンドで設定ファイルの雛形が出力されるのでこれを/etc/supervisord.confに書き出す。

echo_supervisord_conf > /etc/supervisord.conf
includeコンフィグ用のディレクトリ作成
sudo mkdir /etc/supervisord.d
ログ出力先のディレクトリを作成、log rotationの設定も行っておきます。
sudo mkdir /var/log/supervisor/

sudo sh -c "echo '/var/log/supervisor/*.log {
       missingok
       weekly
       notifempty
       nocompress
}' > /etc/logrotate.d/supervisor"

supervisord.conf修正

cp -p /etc/supervisord.conf /etc/supervisord.conf.org

diff /etc/supervisord.conf.org /etc/supervisord.conf
======
28c28,29
< logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
---
> ;logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
> logfile=/var/log/supervisor/supervisord.log
32c33,34
< pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
---
> ;pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
> pidfile=/var/run/supervisord.pid
151c153
< ;[include]
---
> [include]
152a155
> files = supervisord.d/*.ini

※上記の説明

ログディレクトリ変更
;logfile=/tmp/supervisord.log
logfile=/var/log/supervisor/supervisord.log
pid, includeの設定

pidファイルは/var/run/以下に生成するようにし、 /etc/supervisord.d/以下の設定ファイルをincludeできるようにします。

pidファイル
;pidfile=/tmp/supervisord.pid
pidfile=/var/run/supervisord.pid
includeセクションがをイキにして下記の用に修正
[include]
files = supervisord.d/*.ini

supervisord本体のシステムサービス登録

CentOS6系

initスクリプトは以下を使用

https://github.com/Supervisor/initscripts/blob/master/redhat-init-equeffelec

sudo curl -o /etc/rc.d/init.d/supervisord https://raw.githubusercontent.com/Supervisor/initscripts/master/redhat-init-equeffelec
sudo chmod 755 /etc/rc.d/init.d/supervisord
sudo chkconfig --add supervisord
CentOS7系:systemdに登録

/etc/systemd/system/supervisord.serviceを用意

sudo sh -c "echo '[Unit]
Description=Supervisor process control system for UNIX
Documentation=http://supervisord.org
After=network.target

[Service]
ExecStart=/usr/bin/supervisord -n -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=50s

[Install]
WantedBy=multi-user.target' > /etc/systemd/system/supervisord.service"
サービス起動確認
systemctl start supervisord
systemctl status supervisord
systemctl stop supervisord
自動起動に登録
systemctl enable supervisord.service

特定の処理をデーモン化してみる

動作確認用に10秒毎に現在時刻を出力するシェルを用意

/home/hoge/hello.sh

#!/bin/sh
while true 
do
sleep 10s
echo "hello "`date`
done

このシェルを実行するプロセス設定ファイルを以下のように作成

sudo sh -c "echo '[program:hello]
directory=/home/hoge
command=/bin/sh hello.sh
user=root
autorestart=true  ;
stdout_logfile=/home/hoge/hello.log ;
redirect_stderr=true  ;
' > /etc/supervisord.d/hello.ini"
動作確認

tail -f /home/tcmobile/hello.log →タイムスタンプの値が出続けていればOK

デーモンプロセス一覧確認

supervisorctl

hello RUNNING pid 14742, uptime 0:01:43 動作している

プロセスを停止してみる supervisorctl stop hello supervisorctl

hello STOPPED Jun 27 07:02 PM 止まっている

管理画面について

https://debug-life.net/entry/989 WebUIが存在するが今回は置く

Supervisorの検証はここまで