Supervisor-让你的程序监控管理更得心应手

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: Supervisor-让你的程序监控管理更得心应手

Supervisor初探篇


简介


Supervisor是一个客户机/服务器系统(也就是非常经典的C/S结构),它允许用户在类UNIX操作系统上控制许多进程。


优点


  • 简单便捷


通过简单易懂的配置文件配置Supervisor,即可对任务的管理与监控,它提供了重新启动失败的进程和自动日志轮换等功能。


  • 集中


提供了一个开始,停止和监视的地方。可以单独或成组控制过程。配置Supervisor以提供本地或远程命令行和Web界面。


  • 高效


通过fork / exec启动其子进程,并且子进程不守护。进程终止时,操作系统会立即向Supervisor发送信号


  • 可扩展


Supervisor具有一个简单的事件通知协议,该协议可以使用任何语言编写的程序对其进行监视,并且具有用于控制的XML-RPC接口


  • 兼容强


除Windows之外,Supervisor几乎适用于所有事物。它已在Linux,Mac OS X,Solaris和FreeBSD上经过测试和支持。它完全用Python编写


  • 久经考验


尽管Supervisor如今非常活跃,但它不是新软件。Supervisor已经存在了很多年,并且已经在许多服务器上使用


听我说了这么多,相信你也和我一样迫不及待想了解它。嘿呀,不要着急。在学习使用它之前,我们对它的基础组成有个了解,以便于稍后更加得心应手的使用


主要组件


  • supervisord


  • supervisorctl


  • Web Server


  • XML-RPC Interface


在简介中提到Supervisor是一个客户机/服务器系统(也就是非常经典的C/S结构),那么也在此介绍一下各个组件


Supervisord: 服务端的主管被称为supervisord。它负责自行调用启动子程序,响应来自客户端的命令,重新启动崩溃或退出的子进程,记录其子进程stdoutstderr 输出以及生成和处理与子进程生存期中的点相对应的“事件”。


服务器进程使用的配置文件位于/etc/supervisord.conf中,通过适当的文件系统权限确保此文件的安全(它包含未加密的用户名和密码,它可能是我们服务器的root密码)


Supervisorctl:客户端的主管被称为 supervisorctl, 它提供了类似于shell的界面,以便于我们使用命令对服务端的主管进行管理与控制。从supervisorctl我们可以连接到不同的supervisord进程(一次一个),获取受其控制的子进程的状态,停止和启动子进程,以及获取正在运行的supervisord进程的列表


命令行客户机通过UNIX域套接字或internet(TCP)套接字与服务器通信。服务器可以断言客户端的用户应该在允许他执行命令之前提供身份验证凭据。客户机进程通常使用与服务器相同的配置文件,但是任何带有[supervisorctl]节的配置文件都可以工作。


如果在internet套接字上启动supervisord,则可以通过浏览器访问功能类似于supervisorctl web用户界面。访问服务器URL(例如。http://ip:prot)激活配置文件的[inet http server]部分后,通过web界面查看和控制进程状态。


XML-RPC接口:服务于Web UI的同一HTTP服务器提供XML-RPC接口,该接口可用于询问和控制管理程序及其运行的程序。XML-RPC API文档


Supervisor使用技巧篇


Supervisor的安装


supervisor支持了大多包管理工具进行安装


1# pip(推荐)
2pip install supervisor
3# mac 
4brew install supervisor
5# ubantu
6apt install supervisor
7# centos
8yum install supervisor


Supervisor的使用


这里我们建立一个测试用的py文件,如下


1import sys
 2import time
 3from random import uniform
 4from loguru import logger
 5
 6
 7def test():
 8    i = 0
 9    while True:
10        logger.info(i)
11        randomTime = uniform(0, 1)
12        logger.info(f'Sleep {randomTime}s')
13        time.sleep(randomTime)
14        i += 1
15        if i == 10:
16            sys.exit()
17
18
19if __name__ == '__main__':
20    test()


此时可以正常运行


温馨提示:这样的死循环记得一定要给休眠或者退出条件,要不将会有快乐的事情发生哦


运行如下


image.png


对接Supervisor


1# supervisord.conf
 2[supervisord]
 3nodaemon=true                                 ;守护进程,默认fales
 4logfile_maxbytes=50MB        ;日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小
 5logfile_backups=10           ;日志文件保留备份数量默认10,设为0表示不备份
 6loglevel=debug               ;日志级别,默认info,其它: debug,warn,trace
 7
 8
 9[program:demo]
10process_name=tester
11command=python3 demo.py
12autostart=true       ; 在supervisord启动的时候也自动启动
13startsecs=10         ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒


使用supervisord -c supervisord.conf,运行程序。


输出日志


12021-01-12 17:57:21,705 INFO supervisord started with pid 1257
  22021-01-12 17:57:22,712 INFO spawned: 'tester' with pid 1260
  32021-01-12 17:57:22,856 DEBG 'tester' stderr output:
  42021-01-12 17:57:22.855 | INFO     | __main__:test:10 - 0
  52021-01-12 17:57:22.856 | INFO     | __main__:test:12 - Sleep 0.681474110840254s
  6
  72021-01-12 17:57:23,542 DEBG 'tester' stderr output:
  82021-01-12 17:57:23.541 | INFO     | __main__:test:10 - 1
  9
 102021-01-12 17:57:23,542 DEBG 'tester' stderr output:
 112021-01-12 17:57:23.541 | INFO     | __main__:test:12 - Sleep 0.34284895238537105s
 12
 132021-01-12 17:57:23,887 DEBG 'tester' stderr output:
 142021-01-12 17:57:23.886 | INFO     | __main__:test:10 - 2
 15
 162021-01-12 17:57:23,887 DEBG 'tester' stderr output:
 172021-01-12 17:57:23.887 | INFO     | __main__:test:12 - Sleep 0.08220508414530214s
 18
 192021-01-12 17:57:23,970 DEBG 'tester' stderr output:
 202021-01-12 17:57:23.969 | INFO     | __main__:test:10 - 3
 21
 222021-01-12 17:57:23,971 DEBG 'tester' stderr output:
 232021-01-12 17:57:23.970 | INFO     | __main__:test:12 - Sleep 0.39740491822333646s
 24
 252021-01-12 17:57:24,372 DEBG 'tester' stderr output:
 262021-01-12 17:57:24.371 | INFO     | __main__:test:10 - 4
 27
 282021-01-12 17:57:24,372 DEBG 'tester' stderr output:
 292021-01-12 17:57:24.371 | INFO     | __main__:test:12 - Sleep 0.9054854146830564s
 30
 312021-01-12 17:57:25,280 DEBG 'tester' stderr output:
 322021-01-12 17:57:25.279 | INFO     | __main__:test:10 - 5
 33
 342021-01-12 17:57:25,281 DEBG 'tester' stderr output:
 352021-01-12 17:57:25.280 | INFO     | __main__:test:12 - Sleep 0.4563320839294708s
 36
 372021-01-12 17:57:25,742 DEBG 'tester' stderr output:
 382021-01-12 17:57:25.741 | INFO     | __main__:test:10 - 6
 39
 402021-01-12 17:57:25,742 DEBG 'tester' stderr output:
 412021-01-12 17:57:25.741 | INFO     | __main__:test:12 - Sleep 0.19482948337371853s
 42
 432021-01-12 17:57:25,939 DEBG 'tester' stderr output:
 442021-01-12 17:57:25.938 | INFO     | __main__:test:10 - 7
 45
 462021-01-12 17:57:25,940 DEBG 'tester' stderr output:
 472021-01-12 17:57:25.939 | INFO     | __main__:test:12 - Sleep 0.7755167696398192s
 48
 492021-01-12 17:57:26,719 DEBG 'tester' stderr output:
 502021-01-12 17:57:26.718 | INFO     | __main__:test:10 - 8
 51
 522021-01-12 17:57:26,719 DEBG 'tester' stderr output:
 532021-01-12 17:57:26.718 | INFO     | __main__:test:12 - Sleep 0.24748008436152524s
 54
 552021-01-12 17:57:26,972 DEBG 'tester' stderr output:
 562021-01-12 17:57:26.971 | INFO     | __main__:test:10 - 9
 57
 582021-01-12 17:57:26,973 DEBG 'tester' stderr output:
 592021-01-12 17:57:26.972 | INFO     | __main__:test:12 - Sleep 0.6178291278890581s
 60
 612021-01-12 17:57:27,607 DEBG fd 13 closed, stopped monitoring <POutputDispatcher at 140666266690064 for <Subprocess at 140666266191824 with name tester in state STARTING> (stderr)>
 622021-01-12 17:57:27,607 DEBG fd 11 closed, stopped monitoring <POutputDispatcher at 140666266190928 for <Subprocess at 140666266191824 with name tester in state STARTING> (stdout)>
 632021-01-12 17:57:27,608 INFO exited: tester (exit status 0; not expected)
 642021-01-12 17:57:27,608 DEBG received SIGCHLD indicating a child quit
 652021-01-12 17:57:28,613 INFO spawned: 'tester' with pid 1263
 662021-01-12 17:57:28,720 DEBG 'tester' stderr output:
 672021-01-12 17:57:28.719 | INFO     | __main__:test:10 - 0
 682021-01-12 17:57:28.720 | INFO     | __main__:test:12 - Sleep 0.8226218737496696s
 69
 702021-01-12 17:57:29,543 DEBG 'tester' stderr output:
 712021-01-12 17:57:29.542 | INFO     | __main__:test:10 - 1
 72
 732021-01-12 17:57:29,544 DEBG 'tester' stderr output:
 742021-01-12 17:57:29.543 | INFO     | __main__:test:12 - Sleep 0.6507710747677439s
 75
 762021-01-12 17:57:30,195 DEBG 'tester' stderr output:
 772021-01-12 17:57:30.195 | INFO     | __main__:test:10 - 2
 78
 792021-01-12 17:57:30,196 DEBG 'tester' stderr output:
 802021-01-12 17:57:30.195 | INFO     | __main__:test:12 - Sleep 0.3645783421505362s
 81
 822021-01-12 17:57:30,565 DEBG 'tester' stderr output:
 832021-01-12 17:57:30.564 | INFO     | __main__:test:10 - 3
 84
 852021-01-12 17:57:30,565 DEBG 'tester' stderr output:
 862021-01-12 17:57:30.565 | INFO     | __main__:test:12 - Sleep 0.47083797385643844s
 87
 882021-01-12 17:57:31,037 DEBG 'tester' stderr output:
 892021-01-12 17:57:31.036 | INFO     | __main__:test:10 - 4
 90
 912021-01-12 17:57:31,037 DEBG 'tester' stderr output:
 922021-01-12 17:57:31.037 | INFO     | __main__:test:12 - Sleep 0.4875197581833751s
 93
 942021-01-12 17:57:31,531 DEBG 'tester' stderr output:
 952021-01-12 17:57:31.530 | INFO     | __main__:test:10 - 5
 96
 972021-01-12 17:57:31,531 DEBG 'tester' stderr output:
 982021-01-12 17:57:31.530 | INFO     | __main__:test:12 - Sleep 0.9094546698090918s
 99
1002021-01-12 17:57:32,444 DEBG 'tester' stderr output:
1012021-01-12 17:57:32.443 | INFO     | __main__:test:10 - 6
102
1032021-01-12 17:57:32,445 DEBG 'tester' stderr output:
1042021-01-12 17:57:32.444 | INFO     | __main__:test:12 - Sleep 0.47064821128443857s
105
1062021-01-12 17:57:32,921 DEBG 'tester' stderr output:
1072021-01-12 17:57:32.920 | INFO     | __main__:test:10 - 7
108
1092021-01-12 17:57:32,922 DEBG 'tester' stderr output:
1102021-01-12 17:57:32.921 | INFO     | __main__:test:12 - Sleep 0.7673175029063347s
111
1122021-01-12 17:57:33,691 DEBG 'tester' stderr output:
1132021-01-12 17:57:33.690 | INFO     | __main__:test:10 - 8
114
1152021-01-12 17:57:33,692 DEBG 'tester' stderr output:
1162021-01-12 17:57:33.691 | INFO     | __main__:test:12 - Sleep 0.9317641783846109s
117
1182021-01-12 17:57:34,625 DEBG 'tester' stderr output:
1192021-01-12 17:57:34.624 | INFO     | __main__:test:10 - 9
120
1212021-01-12 17:57:34,625 DEBG 'tester' stderr output:
1222021-01-12 17:57:34.625 | INFO     | __main__:test:12 - Sleep 0.2558276039626808s
123
1242021-01-12 17:57:34,899 DEBG fd 13 closed, stopped monitoring <POutputDispatcher at 140666266690000 for <Subprocess at 140666266191824 with name tester in state STARTING> (stderr)>
1252021-01-12 17:57:34,899 DEBG fd 11 closed, stopped monitoring <POutputDispatcher at 140666266689872 for <Subprocess at 140666266191824 with name tester in state STARTING> (stdout)>
1262021-01-12 17:57:34,899 INFO exited: tester (exit status 0; not expected)
1272021-01-12 17:57:34,900 DEBG received SIGCHLD indicating a child quit
1282021-01-12 17:57:36,912 INFO spawned: 'tester' with pid 1264
1292021-01-12 17:57:37,022 DEBG 'tester' stderr output:
1302021-01-12 17:57:37.021 | INFO     | __main__:test:10 - 0
1312021-01-12 17:57:37.021 | INFO     | __main__:test:12 - Sleep 0.5475564566091946s
132
1332021-01-12 17:57:37,572 DEBG 'tester' stderr output:
1342021-01-12 17:57:37.571 | INFO     | __main__:test:10 - 1
135
1362021-01-12 17:57:37,573 DEBG 'tester' stderr output:
1372021-01-12 17:57:37.572 | INFO     | __main__:test:12 - Sleep 0.6326087978849619s
138
1392021-01-12 17:57:38,207 DEBG 'tester' stderr output:
1402021-01-12 17:57:38.206 | INFO     | __main__:test:10 - 2
141
1422021-01-12 17:57:38,207 DEBG 'tester' stderr output:
1432021-01-12 17:57:38.207 | INFO     | __main__:test:12 - Sleep 0.3225720045649825s
144
1452021-01-12 17:57:38,531 DEBG 'tester' stderr output:
1462021-01-12 17:57:38.530 | INFO     | __main__:test:10 - 3
147
1482021-01-12 17:57:38,531 DEBG 'tester' stderr output:
1492021-01-12 17:57:38.530 | INFO     | __main__:test:12 - Sleep 0.5121026075892807s
150
1512021-01-12 17:57:39,044 DEBG 'tester' stderr output:
1522021-01-12 17:57:39.043 | INFO     | __main__:test:10 - 4
153
1542021-01-12 17:57:39,045 DEBG 'tester' stderr output:
1552021-01-12 17:57:39.044 | INFO     | __main__:test:12 - Sleep 0.6613469797067474s
156
1572021-01-12 17:57:39,710 DEBG 'tester' stderr output:
1582021-01-12 17:57:39.709 | INFO     | __main__:test:10 - 5
159
1602021-01-12 17:57:39,710 DEBG 'tester' stderr output:
1612021-01-12 17:57:39.709 | INFO     | __main__:test:12 - Sleep 0.5058071583137449s
162
1632021-01-12 17:57:40,220 DEBG 'tester' stderr output:
1642021-01-12 17:57:40.219 | INFO     | __main__:test:10 - 6
165
1662021-01-12 17:57:40,220 DEBG 'tester' stderr output:
1672021-01-12 17:57:40.219 | INFO     | __main__:test:12 - Sleep 0.2779679640725812s
168
1692021-01-12 17:57:40,502 DEBG 'tester' stderr output:
1702021-01-12 17:57:40.502 | INFO     | __main__:test:10 - 7
171
1722021-01-12 17:57:40,503 DEBG 'tester' stderr output:
1732021-01-12 17:57:40.502 | INFO     | __main__:test:12 - Sleep 0.7282026322383534s
174
1752021-01-12 17:57:41,231 DEBG 'tester' stderr output:
1762021-01-12 17:57:41.231 | INFO     | __main__:test:10 - 8
177
1782021-01-12 17:57:41,232 DEBG 'tester' stderr output:
1792021-01-12 17:57:41.231 | INFO     | __main__:test:12 - Sleep 0.37634579152866654s
180
1812021-01-12 17:57:41,610 DEBG 'tester' stderr output:
1822021-01-12 17:57:41.610 | INFO     | __main__:test:10 - 9
183
1842021-01-12 17:57:41,610 DEBG 'tester' stderr output:
1852021-01-12 17:57:41.610 | INFO     | __main__:test:12 - Sleep 0.02539488384007338s
186
1872021-01-12 17:57:41,660 DEBG fd 13 closed, stopped monitoring <POutputDispatcher at 140666266690256 for <Subprocess at 140666266191824 with name tester in state STARTING> (stderr)>
1882021-01-12 17:57:41,660 DEBG fd 11 closed, stopped monitoring <POutputDispatcher at 140666266689872 for <Subprocess at 140666266191824 with name tester in state STARTING> (stdout)>
1892021-01-12 17:57:41,660 INFO exited: tester (exit status 0; not expected)
1902021-01-12 17:57:41,660 DEBG received SIGCHLD indicating a child quit
1912021-01-12 17:57:44,666 INFO spawned: 'tester' with pid 1265
1922021-01-12 17:57:44,775 DEBG 'tester' stderr output:
1932021-01-12 17:57:44.774 | INFO     | __main__:test:10 - 0
1942021-01-12 17:57:44.774 | INFO     | __main__:test:12 - Sleep 0.540035521075991s
195
1962021-01-12 17:57:45,315 DEBG 'tester' stderr output:
1972021-01-12 17:57:45.315 | INFO     | __main__:test:10 - 1
198
1992021-01-12 17:57:45,316 DEBG 'tester' stderr output:
2002021-01-12 17:57:45.315 | INFO     | __main__:test:12 - Sleep 0.6011099895313317s
201
2022021-01-12 17:57:45,922 DEBG 'tester' stderr output:
2032021-01-12 17:57:45.921 | INFO     | __main__:test:10 - 2
204
2052021-01-12 17:57:45,923 DEBG 'tester' stderr output:
2062021-01-12 17:57:45.922 | INFO     | __main__:test:12 - Sleep 0.5954410741418728s
207
2082021-01-12 17:57:46,521 DEBG 'tester' stderr output:
2092021-01-12 17:57:46.520 | INFO     | __main__:test:10 - 3
210
2112021-01-12 17:57:46,521 DEBG 'tester' stderr output:
2122021-01-12 17:57:46.521 | INFO     | __main__:test:12 - Sleep 0.10471143983800468s
213
2142021-01-12 17:57:46,631 DEBG 'tester' stderr output:
2152021-01-12 17:57:46.630 | INFO     | __main__:test:10 - 4
216
2172021-01-12 17:57:46,632 DEBG 'tester' stderr output:
2182021-01-12 17:57:46.631 | INFO     | __main__:test:12 - Sleep 0.12704017263351186s
219
2202021-01-12 17:57:46,759 DEBG 'tester' stderr output:
2212021-01-12 17:57:46.758 | INFO     | __main__:test:10 - 5
222
2232021-01-12 17:57:46,760 DEBG 'tester' stderr output:
2242021-01-12 17:57:46.759 | INFO     | __main__:test:12 - Sleep 0.26222866859817395s
225
2262021-01-12 17:57:47,025 DEBG 'tester' stderr output:
2272021-01-12 17:57:47.025 | INFO     | __main__:test:10 - 6
228
2292021-01-12 17:57:47,026 DEBG 'tester' stderr output:
2302021-01-12 17:57:47.025 | INFO     | __main__:test:12 - Sleep 0.31215837276333647s
231
2322021-01-12 17:57:47,343 DEBG 'tester' stderr output:
2332021-01-12 17:57:47.343 | INFO     | __main__:test:10 - 7
234
2352021-01-12 17:57:47,344 DEBG 'tester' stderr output:
2362021-01-12 17:57:47.343 | INFO     | __main__:test:12 - Sleep 0.8863919731268238s
237
2382021-01-12 17:57:48,230 DEBG 'tester' stderr output:
2392021-01-12 17:57:48.230 | INFO     | __main__:test:10 - 8
240
2412021-01-12 17:57:48,230 DEBG 'tester' stderr output:
2422021-01-12 17:57:48.230 | INFO     | __main__:test:12 - Sleep 0.6220607701794121s
243
2442021-01-12 17:57:48,858 DEBG 'tester' stderr output:
2452021-01-12 17:57:48.857 | INFO     | __main__:test:10 - 9
246
2472021-01-12 17:57:48,858 DEBG 'tester' stderr output:
2482021-01-12 17:57:48.858 | INFO     | __main__:test:12 - Sleep 0.9545468958914863s
249
2502021-01-12 17:57:49,835 DEBG fd 13 closed, stopped monitoring <POutputDispatcher at 140666266690128 for <Subprocess at 140666266191824 with name tester in state STARTING> (stderr)>
2512021-01-12 17:57:49,835 DEBG fd 11 closed, stopped monitoring <POutputDispatcher at 140666266689872 for <Subprocess at 140666266191824 with name tester in state STARTING> (stdout)>
2522021-01-12 17:57:49,835 INFO exited: tester (exit status 0; not expected)
2532021-01-12 17:57:49,835 DEBG received SIGCHLD indicating a child quit
2542021-01-12 17:57:50,836 INFO gave up: tester entered FATAL state, too many start retries too quickly


image.png


Supervisor番外篇


Supervisorctl常用命令


1# supervisorctl status:查看进程的状态
 2supervisorctl status
 3status <name>           Get status for a single process
 4status <gname>:*        Get status for all processes in a group
 5status <name> <name>    Get status for multiple named processes
 6status                  Get all process status info
 7
 8# supervisorctl start 启动进程
 9start <name>            Start a process
10start <gname>:*         Start all processes in a group
11start <name> <name>     Start multiple processes or groups
12start all               Start all processes
13
14# supervisorctl stop 停止进程
15stop <name>             Stop a process
16stop <gname>:*          Stop all processes in a group
17stop <name> <name>      Stop multiple processes or groups
18stop all                Stop all processes
19
20# supervisorctl restart 重启进程
21restart <name>          Restart a process
22restart <gname>:*       Restart all processes in a group
23restart <name> <name>   Restart multiple processes or groups
24restart all             Restart all processes
25Note: restart does not reread config files. For that, see reread and update.
26
27# supervisorctl update 配置文件修改后可以使用该命令加载新的配置
28update                  Reload config and add/remove as necessary, and will restart affected programs
29update all              Reload config and add/remove as necessary, and will restart affected programs
30update <gname> [...]    Update specific groups
31
32supervisorctl reload: 重新启动配置中的所有程序
33
34# 更多命令可 supervisorctl 进入终端。输入help(?)进行查看


Supervisorctl 参数列表


1supervisorctl -- control applications run by supervisord from the cmd line.
 2
 3Usage: /Users/stringle-004/opt/miniconda3/envs/Proxypool/bin/supervisorctl [options] [action [arguments]]
 4
 5Options:
 6-c/--configuration FILENAME -- configuration file path (searches if not given)
 7-h/--help -- print usage message and exit
 8-i/--interactive -- start an interactive shell after executing commands
 9-s/--serverurl URL -- URL on which supervisord server is listening
10     (default "http://localhost:9001").
11-u/--username USERNAME -- username to use for authentication with server
12-p/--password PASSWORD -- password to use for authentication with server
13-r/--history-file -- keep a readline history (if readline is available)
14
15action [arguments] -- see below
16
17Actions are commands like "tail" or "stop".  If -i is specified or no action is
18specified on the command line, a "shell" interpreting actions typed
19interactively is started.  Use the action "help" to find out about available
20actions.


supervisord.conf配置文件示例


使命令用sudo echo_supervisord_conf > supervisord.conf 进行创建


1; Sample supervisor config file.
  2;
  3; For more information on the config file, please see:
  4; http://supervisord.org/configuration.html
  5;
  6; Notes:
  7;  - Shell expansion ("~" or "$HOME") is not supported.  Environment
  8;    variables can be expanded using this syntax: "%(ENV_HOME)s".
  9;  - Quotes around values are not supported, except in the case of
 10;    the environment= options as shown below.
 11;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
 12;  - Command will be truncated if it looks like a config file comment, e.g.
 13;    "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".
 14;
 15; Warning:
 16;  Paths throughout this example file use /tmp because it is available on most
 17;  systems.  You will likely need to change these to locations more appropriate
 18;  for your system.  Some systems periodically delete older files in /tmp.
 19;  Notably, if the socket file defined in the [unix_http_server] section below
 20;  is deleted, supervisorctl will be unable to connect to supervisord.
 21
 22[unix_http_server]
 23file=/tmp/supervisor.sock   ; the path to the socket file
 24;chmod=0700                 ; socket file mode (default 0700)
 25;chown=nobody:nogroup       ; socket file uid:gid owner
 26;username=user              ; default is no username (open server)
 27;password=123               ; default is no password (open server)
 28
 29; Security Warning:
 30;  The inet HTTP server is not enabled by default.  The inet HTTP server is
 31;  enabled by uncommenting the [inet_http_server] section below.  The inet
 32;  HTTP server is intended for use within a trusted environment only.  It
 33;  should only be bound to localhost or only accessible from within an
 34;  isolated, trusted network.  The inet HTTP server does not support any
 35;  form of encryption.  The inet HTTP server does not use authentication
 36;  by default (see the username= and password= options to add authentication).
 37;  Never expose the inet HTTP server to the public internet.
 38
 39;[inet_http_server]         ; inet (TCP) server disabled by default
 40;port=9001        ; ip_address:port specifier, *:port for all iface
 41;username=user              ; default is no username (open server)
 42;password=123               ; default is no password (open server)
 43
 44[supervisord]
 45logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
 46logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
 47logfile_backups=10           ; of main logfile backups; 0 means none, default 10
 48loglevel=info                ; log level; default info; others: debug,warn,trace
 49pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
 50nodaemon=false               ; start in foreground if true; default false
 51minfds=1024                  ; min. avail startup file descriptors; default 1024
 52minprocs=200                 ; min. avail process descriptors;default 200
 53;umask=022                   ; process file creation umask; default 022
 54;user=supervisord            ; setuid to this UNIX account at startup; recommended if root
 55;identifier=supervisor       ; supervisord identifier, default is 'supervisor'
 56;directory=/tmp              ; default is not to cd during start
 57;nocleanup=true              ; don't clean up tempfiles at start; default false
 58;childlogdir=/tmp            ; 'AUTO' child log dir, default $TEMP
 59;environment=KEY="value"     ; key value pairs to add to environment
 60;strip_ansi=false            ; strip ansi escape codes in logs; def. false
 61
 62; The rpcinterface:supervisor section must remain in the config file for
 63; RPC (supervisorctl/web interface) to work.  Additional interfaces may be
 64; added by defining them in separate [rpcinterface:x] sections.
 65
 66[rpcinterface:supervisor]
 67supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
 68
 69; The supervisorctl section configures how supervisorctl will connect to
 70; supervisord.  configure it match the settings in either the unix_http_server
 71; or inet_http_server section.
 72
 73[supervisorctl]
 74serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
 75;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
 76;username=chris              ; should be same as in [*_http_server] if set
 77;password=123                ; should be same as in [*_http_server] if set
 78;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
 79;history_file=~/.sc_history  ; use readline history if available
 80
 81; The sample program section below shows all possible program subsection values.
 82; Create one or more 'real' program: sections to be able to control them under
 83; supervisor.
 84
 85;[program:theprogramname]
 86;command=/bin/cat              ; the program (relative uses PATH, can take args)
 87;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
 88;numprocs=1                    ; number of processes copies to start (def 1)
 89;directory=/tmp                ; directory to cwd to before exec (def no cwd)
 90;umask=022                     ; umask for process (default None)
 91;priority=999                  ; the relative start priority (default 999)
 92;autostart=true                ; start at supervisord start (default: true)
 93;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
 94;startretries=3                ; max # of serial start failures when starting (default 3)
 95;autorestart=unexpected        ; when to restart if exited after running (def: unexpected)
 96;exitcodes=0                   ; 'expected' exit codes used with autorestart (default 0)
 97;stopsignal=QUIT               ; signal used to kill process (default TERM)
 98;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
 99;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
100;killasgroup=false             ; SIGKILL the UNIX process group (def false)
101;user=chrism                   ; setuid to this UNIX account to run the program
102;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
103;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
104;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
105;stdout_logfile_backups=10     ; # of stdout logfile backups (0 means none, default 10)
106;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
107;stdout_events_enabled=false   ; emit events on stdout writes (default false)
108;stdout_syslog=false           ; send stdout to syslog with process name (default false)
109;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
110;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
111;stderr_logfile_backups=10     ; # of stderr logfile backups (0 means none, default 10)
112;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
113;stderr_events_enabled=false   ; emit events on stderr writes (default false)
114;stderr_syslog=false           ; send stderr to syslog with process name (default false)
115;environment=A="1",B="2"       ; process environment additions (def no adds)
116;serverurl=AUTO                ; override serverurl computation (childutils)
117
118; The sample eventlistener section below shows all possible eventlistener
119; subsection values.  Create one or more 'real' eventlistener: sections to be
120; able to handle event notifications sent by supervisord.
121
122;[eventlistener:theeventlistenername]
123;command=/bin/eventlistener    ; the program (relative uses PATH, can take args)
124;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
125;numprocs=1                    ; number of processes copies to start (def 1)
126;events=EVENT                  ; event notif. types to subscribe to (req'd)
127;buffer_size=10                ; event buffer queue size (default 10)
128;directory=/tmp                ; directory to cwd to before exec (def no cwd)
129;umask=022                     ; umask for process (default None)
130;priority=-1                   ; the relative start priority (default -1)
131;autostart=true                ; start at supervisord start (default: true)
132;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
133;startretries=3                ; max # of serial start failures when starting (default 3)
134;autorestart=unexpected        ; autorestart if exited after running (def: unexpected)
135;exitcodes=0                   ; 'expected' exit codes used with autorestart (default 0)
136;stopsignal=QUIT               ; signal used to kill process (default TERM)
137;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
138;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
139;killasgroup=false             ; SIGKILL the UNIX process group (def false)
140;user=chrism                   ; setuid to this UNIX account to run the program
141;redirect_stderr=false         ; redirect_stderr=true is not allowed for eventlisteners
142;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
143;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
144;stdout_logfile_backups=10     ; # of stdout logfile backups (0 means none, default 10)
145;stdout_events_enabled=false   ; emit events on stdout writes (default false)
146;stdout_syslog=false           ; send stdout to syslog with process name (default false)
147;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
148;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
149;stderr_logfile_backups=10     ; # of stderr logfile backups (0 means none, default 10)
150;stderr_events_enabled=false   ; emit events on stderr writes (default false)
151;stderr_syslog=false           ; send stderr to syslog with process name (default false)
152;environment=A="1",B="2"       ; process environment additions
153;serverurl=AUTO                ; override serverurl computation (childutils)
154
155; The sample group section below shows all possible group values.  Create one
156; or more 'real' group: sections to create "heterogeneous" process groups.
157
158;[group:thegroupname]
159;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
160;priority=999                  ; the relative start priority (default 999)
161
162; The [include] section can just contain the "files" setting.  This
163; setting can list multiple files (separated by whitespace or
164; newlines).  It can also contain wildcards.  The filenames are
165; interpreted as relative to this file.  Included files *cannot*
166; include files themselves.
167
168;[include]
169;files = relative/directory/*.ini


常用配置


1[unix_http_server]
 2file=/tmp/supervisor.sock   ;UNIX socket 文件,supervisorctl 会使用
 3;chmod=0700                 ;socket文件的mode,默认是0700
 4;chown=nobody:nogroup       ;socket文件的owner,格式:uid:gid
 5
 6;[inet_http_server]         ;HTTP服务器,提供web管理界面
 7;port=9001                            ;Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
 8;username=user              ;登录管理后台的用户名
 9;password=123               ;登录管理后台的密码
10
11[supervisord]
12logfile=/tmp/supervisord.log ;日志文件,默认是 $CWD/supervisord.log
13logfile_maxbytes=50MB        ;日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小
14logfile_backups=10           ;日志文件保留备份数量默认10,设为0表示不备份
15loglevel=info                ;日志级别,默认info,其它: debug,warn,trace
16pidfile=/tmp/supervisord.pid ;pid 文件
17nodaemon=false               ;是否在前台启动,默认是false,即以 daemon 的方式启动
18minfds=1024                  ;可以打开的文件描述符的最小值,默认 1024
19minprocs=200                 ;可以打开的进程数的最小值,默认 200
20
21[supervisorctl]
22serverurl=unix:///tmp/supervisor.sock ;通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致
23;serverurl=http://127.0.0.1:9001 ; 通过HTTP的方式连接supervisord
24
25; [program:xx]是被管理的进程配置参数,xx是进程的名称
26[program:xx]
27command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run  ; 程序启动命令
28autostart=true       ; 在supervisord启动的时候也自动启动
29startsecs=10         ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
30autorestart=true     ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
31startretries=3       ; 启动失败自动重试次数,默认是3
32user=tomcat          ; 用哪个用户启动进程,默认是root
33priority=999         ; 进程启动优先级,默认999,值小的优先启动
34redirect_stderr=true ; 把stderr重定向到stdout,默认false
35stdout_logfile_maxbytes=20MB  ; stdout 日志文件大小,默认50MB
36stdout_logfile_backups = 20   ; stdout 日志文件备份数,默认是10
37; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
38stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
39stopasgroup=false     ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
40killasgroup=false     ;默认为false,向进程组发送kill信号,包括子进程
41
42;包含其它配置文件
43[include]
44files = relative/directory/*.ini    ;可以指定一个或多个以.ini结束的配置文件


总结


  • 我们从supervisor基础模型,组成、再到到使用进行了介绍


  • 验证了supervisor确实可以监控我们的任务,且给予了简单的控制面板,更加便于我们控制、监控


  • supervisor的配置文件十分重要,是熟练使用的前提与基石
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
6月前
|
监控 Ruby
使用Ruby编写的电脑监控软件:自动化任务管理与运行状态监测
本文介绍了一款使用Ruby编写的电脑监控软件,该软件通过自动化任务管理和系统状态监测提供便利的系统管理。示例代码展示了如何定义任务类、运行任务、检查系统状态并在异常时发送通知。此外,还说明了如何将监控数据自动提交到网站以进行进一步分析,从而确保系统稳定运行。
307 0
|
6月前
|
监控 安全 Shell
Shell脚本实现企业电脑屏幕监控的自动化部署与维护
企业信息安全一直是重要的议题,而屏幕监控是一种有效的手段之一。本文将介绍如何使用Shell脚本实现企业电脑屏幕监控的自动化部署与维护,并在结尾部分说明如何将监控到的数据自动提交到指定网站。
251 1
|
11月前
|
监控 Java 关系型数据库
『Jmeter超级干货』| Linux下Jmeter安装配置、脚本设计执行、监控及报告完整过程
『Jmeter超级干货』| Linux下Jmeter安装配置、脚本设计执行、监控及报告完整过程
1767 1
|
2月前
|
分布式计算 监控 Hadoop
监控Hadoop集群实战篇
介绍了监控Hadoop集群的方法,包括监控Linux服务器、Hadoop指标、使用Ganglia监控Hadoop集群、Hadoop日志记录、通过Hadoop的Web UI进行监控以及其他Hadoop组件的监控,并提供了相关监控工具和资源的推荐阅读链接。
80 2
|
4月前
|
Ubuntu Unix Linux
如何使用 Supervisor 管理你的进程
**Supervisor** 是一款Python编写的进程管理工具,用于类Unix系统,确保应用服务持续运行。常用命令包括:`reload`(重新加载配置)、`status`(查看进程状态)、`shutdown`(关闭所有进程)、`start`和`stop`(控制单个进程)。在CentOS上安装Supervisor用`yum install -y supervisor`,配置文件通常在`/etc/supervisord.conf`。
72 0
|
JSON 监控 数据格式
实时进程监控:Python脚本的开发与应用
Python作为一种多才多艺的编程语言,为开发实时进程监控工具提供了广泛的可能性。本文将介绍如何使用Python开发一个简单但强大的实时进程监控工具,以及示例代码的应用。
273 0
|
监控 JavaScript
nodejs服务后台持续运行
nodejs服务后台持续运行
205 0
|
负载均衡 监控 JavaScript
后台服务守护进程神器pm2介绍及使用
后台服务守护进程神器pm2介绍及使用
|
消息中间件 NoSQL 前端开发
应用系统服务安装全过程(从基础搭建框架到部署运行)
应用系统服务安装全过程(从基础搭建框架到部署运行)
232 0
应用系统服务安装全过程(从基础搭建框架到部署运行)
|
监控 安全 Java
使用supervisor管理你的程序
Supervisor是python开发的一种进程管理工具。不仅可以监控我们的程序还可以自动重启我们的程序,非常好用。
213 1
使用supervisor管理你的程序
下一篇
无影云桌面