supervisor使用小计(配合ngrok)

搭建ngrok所需要的服务器,推荐阿里云

阿里云高性能服务器2折起:点我购买

近日,服务器的ngrok服务总是过一段时间就会停止,为了解决此问题,需要使用supervisor监控ngrok当其服务停止后,自动重启。现将配置过程记录如下。

安装supervisor (ubuntu)

 apt-get install supervisor

或者

pip install supervisor

创建配置文件

cd /etc
echo_supervisord_conf > supervisord.conf

这样就在/etc目录下建立了默认的supervisor的全局配置文件
在aws上使用上述命令是失败,需要使用下列命令创建:

sudo su - root -c "echo_supervisord_conf > /etc/supervisord.conf"

修改主配置文件

具体supervisor的使用方法请看附录或者自行查阅官方文档,这里仅给出我自己需要修改的地方

[inet_http_server]         ; 开启web控制页面
port=0.0.0.0:9001        ; (ip_address:port specifier, *:port for all iface)
username=root              ; (default is no username (open server))
password=123456             ; (default is no password (open server))

[supervisorctl]
;serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=http://127.0.0.1:9001 ;这个是supervisorctl远程连接supervisord的时候,用到的TCP socket路径注意这个和前面的[inet_http_server]对应
username=user              ; should be same as http_username if set
password=123456               ; 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/supervisor/*.conf

修改进程配置文件

vim /etc/supervisor/test_http.conf

守护进程配置文件内容:

[program:ngrokd]
command = /usr/local/src/ngrok/bin/ngrokd -domain="***" -httpAddr=":8000"
;directory = /usr/local/src/ngrok/bin
process_name = %(program_name)s_%(process_num)s
numprocs = 1 ;注意这里只能为1
autostart = true
autorestart = true
stdout_logfile = /var/log/supervisor/ngrok_stdout.log
stdout_logfile_maxbytes = 10MB
stderr_logfile = /var/log/supervisor/ngrok_error.log
stderr_logfile_maxbytes = 10MB

启动supervisor

配置文件在/etc/supervisord.conf

supervisord -c supervisord.conf 

开机启动

我们需要在 / etc 下编辑 rc.local 的文件 ,让 supervisor 开机启动。这样就可以使脚本在开机的时候随supervisor启动运行

sudo gedit /etc/rc.local

在这个配置文件的 exit 0 前面一行加上 service supervisor start 保存。

其他

supervisorctl status 查看supervisor运行状态
使用上面的配置可以通过 http://ip地址:9001 访问web控制页面

至此配置完成,supervisor开始监控ngrok服务

附 supervisor配置说明

; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Note: shell expansion ("~" or "$HOME") is not supported.  Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".

[unix_http_server]          ; supervisord的unix socket服务配置
file=/tmp/supervisor.sock   ; socket文件的保存目录
;chmod=0700                 ; socket的文件权限 (default 0700)
;chown=nobody:nogroup       ; socket的拥有者和组名
;username=user              ; 默认不需要登陆用户 (open server)
;password=123               ; 默认不需要登陆密码 (open server)

;[inet_http_server]         ; supervisord的tcp服务配置
;port=127.0.0.1:9001        ; tcp端口
;username=user              ; tcp登陆用户
;password=123               ; tcp登陆密码

[supervisord]                ; supervisord的主进程配置
logfile=/tmp/supervisord.log ; 主要的进程日志配置
logfile_maxbytes=50MB        ; 最大日志体积,默认50MB
logfile_backups=10           ; 日志文件备份数目,默认10
loglevel=info                ; 日志级别,默认info; 还有:debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord的pidfile文件
nodaemon=false               ; 是否以守护进程的方式启动
minfds=1024                  ; 最小的有效文件描述符,默认1024
minprocs=200                 ; 最小的有效进程描述符,默认200
;umask=022                   ; 进程文件的umask,默认200
;user=chrism                 ; 默认为当前用户,如果为root则必填
;identifier=supervisor       ; supervisord的表示符, 默认时'supervisor'
;directory=/tmp              ; 默认不cd到当前目录
;nocleanup=true              ; 不在启动的时候清除临时文件,默认false
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value       ; 初始键值对传递给进程
;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              ; 如果设置应该与http_username相同
;password=123                ; 如果设置应该与http_password相同
;prompt=mysupervisor         ; 命令行提示符,默认"supervisor"
;history_file=~/.sc_history  ; 命令行历史纪录

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; 运行的程序 (相对使用PATH路径, 可以使用参数)
;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s
;numprocs=1                    ; 默认启动的进程数目,默认为1
;directory=/tmp                ; 在运行前cwd到指定的目录,默认不执行cmd
;umask=022                     ; 进程umask,默认None
;priority=999                  ; 程序运行的优先级,默认999
;autostart=true                ; 默认随supervisord自动启动,默认true
;autorestart=unexpected        ; whether/when to restart (default: unexpected)
;startsecs=1                   ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 期望的退出码,默认0,2
;stopsignal=QUIT               ; 杀死进程的信号,默认TERM
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; 向unix进程组发送停止信号,默认false
;killasgroup=false             ; 向unix进程组发送SIGKILL信号,默认false
;user=chrism                   ; 为运行程序的unix帐号设置setuid
;redirect_stderr=true          ; 将标准错误重定向到标准输出,默认false
;stdout_logfile=/a/path        ; 标准输出的文件路径NONE=none;默认AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A=1,B=2           ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; 运行的程序 (相对使用PATH路径, 可以使用参数)
;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s
;numprocs=1                    ; 默认启动的进程数目,默认为1
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; 事件缓冲区队列大小,默认10
;directory=/tmp                ; 在运行前cwd到指定的目录,默认不执行cmd
;umask=022                     ; 进程umask,默认None
;priority=-1                   ; 程序运行的优先级,默认-1
;autostart=true                ; 默认随supervisord自动启动,默认true
;autorestart=unexpected        ; whether/when to restart (default: unexpected)
;startsecs=1                   ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 期望的退出码,默认0,2
;stopsignal=QUIT               ; 杀死进程的信号,默认TERM
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; 向unix进程组发送停止信号,默认false
;killasgroup=false             ; 向unix进程组发送SIGKILL信号,默认false
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups        ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A=1,B=2           ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; 任何在[program:x]中定义的x
;priority=999                  ; 程序运行的优先级,默认999

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

;[include]
;files = relative/directory/*.ini
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,980评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,422评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,130评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,553评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,408评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,326评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,720评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,373评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,678评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,722评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,486评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,335评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,738评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,283评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,692评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,893评论 2 335

推荐阅读更多精彩内容