Skip to content

Instantly share code, notes, and snippets.

@xluffy
Last active February 21, 2021 09:08
Show Gist options
  • Save xluffy/14d87b7dfd6195705cd9 to your computer and use it in GitHub Desktop.
Save xluffy/14d87b7dfd6195705cd9 to your computer and use it in GitHub Desktop.
supervisord

Supervisord

1. Giới thiệu

Supervisord là công cụ quản lý unix process

Một trong những ứng dụng của Supervisor là quản lý background process dạng Worker.

  • Crawler worker
  • Các service như gearmand ..

Mục đích quản lý:

  • Kiểm soát số lượng worker cùng chạy
  • Keep alive, restart nếu gặp lỗi
  • Monitor & Alert (dùng Superlance, sẽ đề cập dưới đây)

2. Cài đặt và cấu hình

B1: Cài đặt

yum install python-setuptools
easy_install supervisor

B2: Cấu hình cơ bản

echo_supervisord_conf > /etc/supervisord.conf
cat > /etc/supervisord.conf<<EOF
[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                 ; socket file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/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=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false               ; (start in foreground if true;default false)

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

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

[include]
files = /etc/supervisor.d/*.conf
EOF
mkdir /etc/supervisor.d
mkdir /var/log/supervisor

Cấu hình một chương trình tên là poc

cat > /etc/supervisor.d/poc.conf<<EOF
[program:poc_process]
command=/root/test.sh
autostart=true
autorestart=true
startretries=3
stderr_logfile=/var/log/supervisor/poc_process.err.log
stdout_logfile=/var/log/supervisor/poc_process.out.log
EOF
#!/bin/bash
echo starting to sleep...
sleep 10

Lưu ý: Tên process phải là duy nhất, vì vậy nếu numprocs > 1 thì phải định nghĩa lại tên process, dạng như sau:

process_name=%(program_name)s_%(process_num)02d

process_name được tạo ra có dạng như sau:

poc_process:poc_process_01
poc_process:poc_process_02

poc_process có thể đóng vai trò như 1 group: supervisorctl start poc_process sẽ có tác dụng trên tất cả các child process, nhưng cũng có thể thao tác trên từng child process: supervisorctl stop poc_process:poc_process_01

wget -O /etc/init.d/supervisord https://gist.githubusercontent.com/xluffy/efb82d45cab2dd1bec99/raw/75136a80b20a9a1478edc77aa37fd12f6e7080ae/supervisord-init.sh
chmod +x etc/init.d/supervisord 

Web Interface

[inet_http_server]
port = 9001
username = user # Basic auth username
password = pass # Basic auth password

3. Quản trị

Start supervisord

/etc/init.d/supervisord start

Đăng nhập vào giao diện quản trị

supervisorctl

Một số lệnh trên giao diện quản trị

# supervisorctl 
> status poc   RUNNING   pid 36276, uptime 0:18:17
> start <program_name>
> stop <program_name>
> restart <program_name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment