LoginSignup
2
6

More than 3 years have passed since last update.

CentOS7系環境でsupervisorを使ってphpをデーモン化してみた

Posted at

supervisorとは

supervisorとは、プロセスの死活監視などできるデーモン化ツールの1つです。

  • プロセスの死活監視し、プロセスが寝たら再起動させる
  • stdout/stderrを出力させる
  • ステータス確認もできる

導入手順

まずは、supervisorをインストールします。
今回はEPELリポジトリからを前提に書いています。
(EPELリポジトリ経由で無い場合、「$ mkdir /etc/supervisor.d」など少し作業が増える)

$ yum install supervisor

initスクリプトはredhat-init-equeffelecを利用します。

$ curl -o /etc/rc.d/init.d/supervisord https://raw.githubusercontent.com/Supervisor/initscripts/master/redhat-init-equeffelec
$ chmod 755 /etc/rc.d/init.d/supervisord

log rotationを作成します。

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

supervisord.confに動かしたいプログラムを追記します。
(細かい詳細はググってください。)

$ vi /etc/supervisord.conf

 1| [program:Execute001]
 2| command=/usr/bin/php /var/www/html/batch/Execute001.php
 3| ;priority=999                ; the relative start priority (default 999)
 4| autostart=true              ; start at supervisord start (default: true)
 5| autorestart=true            ; retstart at unexpected quit (default: true)
 6| ;startsecs=10                ; number of secs prog must stay running (def. 10)
 7| ;startretries=3              ; max # of serial start failures (default 3)
 8| ;exitcodes=0,2               ; expected exit codes for process (default 0,2)
 9| ;stopsignal=QUIT             ; signal used to kill process (default TERM)
10| ;stopwaitsecs=10             ; max num secs to wait before SIGKILL (default 10)
11| ;user=chrism                 ; setuid to this UNIX account to run the program
12| log_stdout=true             ; if true, log program stdout (default true)
13| log_stderr=true             ; if true, log program stderr (def false)
14| logfile=/var/log/supervisor/sampledeamon.log
15| logfile_maxbytes=1MB        ; max # logfile bytes b4 rotation (default 50MB)
16| logfile_backups=10          ; # of logfile backups (default 10)

サービスを実行し、状態を確認します。

$ service supervisord start
$ service supervisord status

サービスを止めたいときは、以下を実行します。

$ service supervisord stop
2
6
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
2
6