LoginSignup
48
48

More than 5 years have passed since last update.

Supervisor を利用して Go アプリケーションをデーモン化する

Posted at

環境

Amazon Linux

Supervisor をインストールする

Amazon Linux の場合、既に easy_install がインストールされている。

sudo easy_install supervisor

開発した Go アプリケーションをデプロイする

手順はお好きなように。
後述の設定ファイルに起動コマンドを指定するため、go build はしておくこと。

Supervisor の設定ファイルを作成する

/etc/supervisord.conf に Supervisor 自身の設定と、
開発した Go アプリケーションの設定を記述する。

※ 設定内容については以下のサイトを参考にさせて頂きました
https://github.com/astaxie/build-web-application-with-golang/blob/master/ja/ebook/12.3.md

;/etc/supervisord.conf
[unix_http_server]
file = /tmp/supervisor.sock
chmod = 0777
chown= root:root

[inet_http_server]
port=9001
username = admin
password = yourpassword

[supervisorctl]
serverurl = unix:///tmp/supervisor.sock

[supervisord]
logfile=/var/log/supervisord/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
pidfile=/var/run/supervisord.pid
nodaemon=true
minfds=1024
minprocs=200
user=root
childlogdir=/var/log/supervisord/

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

; 自分が開発したアプリケーションの設定
[program:yourapp]
command=/path-to-path/yourappname
autostart = true
startsecs = 5
user = root
redirect_stderr = true
stdout_logfile = /var/log/supervisord/blogdemon.log

Supervisor を起動

supervisord を叩いて Supervisor を起動する。

supervisord

正常に起動すると、以下の様なログが出力される。
以降は supervisorctl で起動・停止等の操作を行うことができるようになる。

2014-10-05 15:11:27,197 CRIT Set uid to user 0
2014-10-05 15:11:27,206 INFO RPC interface 'supervisor' initialized
2014-10-05 15:11:27,207 INFO RPC interface 'supervisor' initialized
2014-10-05 15:11:27,207 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2014-10-05 15:11:27,207 INFO supervisord started with pid 23899
2014-10-05 15:11:28,209 INFO spawned: 'yourappname' with pid 23902
2014-10-05 15:11:33,225 INFO success: yourappname entered RUNNING state, process has stayed up for > than 5 seconds (startsecs)

Supervisor の自動起動設定

git clone git://github.com/Supervisor/initscripts.git
cd initscripts/
cp redhat-init-jkoppe /etc/init.d/supervisord
cp redhat-sysconfig-jkoppe /etc/sysconfig/supervisord
chkconfig --add supervisord
chkconfig supervisord on
48
48
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
48
48