適当なスクリプトをデーモン化するのにSupervisorが便利
適当なスクリプトをデーモン化しようと思った時の典型的な要件が以下であるが、この記事でも紹介したpython製のプロセス管理ツールであるSupervisorを使うことによって解決できる。
- プロセスの生死の監視する
- プロセスが死んだら勝手に再起動する
- 標準出力やエラー出力のログを取る
- 場合によっては複数プロセスを起動したい
- プロセスのステータスを簡単に確認したい
この記事では、プロセス管理ツールSupervisorの導入を簡単に紹介する。
インストール
easy_installからインストールできる。そもそもeasy_installが入ってない場合は以下みたいにインストール。
$ curl -O http://peak.telecommunity.com/dist/ez_setup.py $ python ez_setup.py
Supervisorをeasy_installからインストールします。
$ easy_install supervisor
Supervisor用の設定ファイルを/etc/supervisord.confに書き出して適当に修正。
$ echo_supervisord_conf > /etc/supervisord.conf $ vim /etc/supervisor.conf $ cat /etc/supervisor.conf [unix_http_server] file=/tmp/supervisor.sock ; (the path to the socket file) ;chmod=0700 ; socket file mode (default 0700) ;chown=nobody:nogroup ; socket file uid:gid owner ;username=user ; (default is no username (open server)) ;password=123 ; (default is no password (open server)) ;[inet_http_server] ; inet (TCP) server disabled by default ;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface) ;port=172.16.1.20:9001 ; (ip_address:port specifier, *:port for all iface) ;username=hoge ; (default is no username (open server)) ;password=hoge ; (default is no password (open server)) [supervisord] logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log) logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) logfile_backups=10 ; (num of main logfile rotation backups;default 10) loglevel=info ; (log level;default info; others: debug,warn,trace) pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid) nodaemon=false ; (start in foreground if true;default false) minfds=1024 ; (min. avail startup file descriptors;default 1024) minprocs=200 ; (min. avail process descriptors;default 200) ;umask=022 ; (process file creation umask;default 022) ;user=chrism ; (default is current user, required if root) ;identifier=supervisor ; (supervisord identifier, default is 'supervisor') ;directory=/tmp ; (default is not to cd during start) ;nocleanup=true ; (don't clean up tempfiles at start;default false) ;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP) ;environment=KEY=value ; (key value pairs to add to environment) ;strip_ansi=false ; (strip ansi escape codes in logs; def. false) ; the below section must remain in the config file for RPC ; (supervisorctl/web interface) to work, additional interfaces may be ; added by defining them in separate rpcinterface: sections [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket ;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket ;username=chris ; should be same as http_username if set ;password=123 ; should be same as http_password if set ;prompt=mysupervisor ; cmd line prompt (default "supervisor") ;history_file=~/.sc_history ; use readline history if available [include] files = /etc/supervisord.d/*.ini
設定用のディレクトリを作って、このディレクトリ以下にiniファイルを置いてやってsupervisordで管理するプロセスを設定する。
$ mkdir /etc/supervisord.d
起動して特に文句言われなかったらインストール成功。
$ supervisord
注意としてはSupervisorをインストールしてもsupervisord自体の自動起動の設定はまだなされていないのでそのへんは各自でやる。とはいえsupervisordのプロセスを起動したりステータスを見たり再起動したりしたい場合はsupervisorctlというコマンドを使えばいいので、/etc/init.d/rc.localの中でsupevisordを起動するようにするだけでも問題ないと思う。
管理するプロセスの設定
例えば、tiarraを管理するための設定は以下みたいになる。
[program:tiarra] command=/path/to/tiarra --config=/path/to/tiarra.conf numprocs=1 autostart=true ; supervisorが起動したら自動的に起動する autorestart=true ; 落ちても自動的に再起動する user=hoge redirect_stderr=true ; エラー出力を標準出力にリダイレクトする stdout_logfile=/path/to/tiarra.log ; 標準出力をログに取る
これを/etc/supervisord.d/tiarra.confに置いて、Supervisorを再起動して背泳させる。
$ supervisorctl restart
supervisordを再起動せずに追加したい場合は、以下みたいにrereadで設定ファイルを再読み込みしてからaddする。
$ supervisorctl reread $ supervisorctl add tiarra
起動したプロセスはstatusコマンドで確認できる。
$ supervisorctl status tiarra RUNNING pid 18350, uptime 28 days, 6:12:57
supervisorで管理するプロセスの注意としては、フォアグラウンドで起動するプロセスのみしか管理できない。すぐにバックグラウンドに回るようなものを管理したい場合は、フォアグラウンドで起動するようなオプションや設定が有るはずなのでそれを利用する。例えばnginxの場合だと設定ファイルに`daemon off;`を追加するとうまくいく。
プロセスの管理
Supervisorで管理しているプロセスを確認する。
$ supervisorctl status hubot RUNNING pid 11154, uptime 1 day, 13:51:11 hubot-proxy RUNNING pid 13157, uptime 20 days, 5:10:37 ikachan RUNNING pid 18347, uptime 28 days, 6:12:57 tiarra RUNNING pid 18350, uptime 28 days, 6:12:57 weinre RUNNING pid 18348, uptime 28 days, 6:12:57
適当にあるプロセスを再起動してみる。
$ supervisorctl restart hubot hubot: stopped hubot: started
ログをtailしてみる。オプションの数値は、表示するログの行数ではなくバイト数。
$ supervisorctl tail -1000 tiarra of the client at localhost(127.0.0.1). [pid:13157 2012/03/07 21:36:44] Accepted login of hubot!nodebot@localhost, from localhost(127.0.0.1). [pid:13157 2012/03/07 21:40:10] Disconnected Client from localhost(127.0.0.1). [pid:13157 2012/03/08 14:26:57] One client at 127.0.0.1 connected to me. Please wait to get hostname of this address. [pid:13157 2012/03/08 14:26:59] Accepted connection of the client at localhost(127.0.0.1). [pid:13157 2012/03/08 14:26:59] Accepted login of hubot!nodebot@localhost, from localhost(127.0.0.1). [pid:13157 2012/03/08 14:30:00] Disconnected Client from localhost(127.0.0.1). [pid:13157 2012/03/10 04:19:02] One client at 127.0.0.1 connected to me. Please wait to get hostname of this address. [pid:13157 2012/03/10 04:19:04] Accepted connection of the client at localhost(127.0.0.1). [pid:13157 2012/03/10 04:19:04] Accepted login of hubot!nodebot@localhost, from localhost(127.0.0.1). [pid:13157 2012/03/10 04:20:00] Disconnected Client from localhost(127.0.0.1).
tail -f もできる。
$ supervisorcrl tail -f tiarra