最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】CentOS中如何用systemctl管理supervisor包括设置supervisor开机启动

CentOS crifan 3753浏览 0评论

折腾:

【已解决】升级CentOS中的supervisor到最新版本

期间,由于在CentOS中,卸载了旧的用yum安装的supervisor后,

其中:yum安装的supervisor自带支持systemctl控制,且配置文件在/etc/下面

使得需要去手动添加配置文件,以实现,支持systemctl去管理supervisor,包括设置开机启动

去看看如何用systemctl去管理

centos systemctl manage supervisor

centos systemctl manager supervisor

zokeber/supervisor-systemd: This service is for Systemd (system and service manager) in CentOS 7 / Fedora 24

解释的很清楚

supervisor-systemd/supervisord.service at master · zokeber/supervisor-systemd

里面有个:

/usr/bin/supervisord -n

去看了看-n是nodaemon:

<code>[root@xx-general-01 robotDemo]# /usr/bin/supervisord --help
supervisord -- run a set of applications as daemons.

Usage: /usr/bin/supervisord [options]

Options:
-c/--configuration FILENAME -- configuration file
-n/--nodaemon -- run in the foreground (same as 'nodaemon true' in config file)
</code>

supervisor-systemd/example-nodejs.conf.bak at master · zokeber/supervisor-systemd

好像是:

其中program部分都可以自己定义environment?

supervisor-systemd/supervisord.conf at master · zokeber/supervisor-systemd

install and configure supervisord on centos 7.

提到了:

systemctl start supervisord

之前要:

systemctl daemon-reload

此处没有给/bin/supervisord加上-n,是一般正常做法

How to use supervisord in centos/fedora/red hat like systems

然后去:

【整理】systemctl的使用心得和语法

这是前面就看到的配置:

initscripts/centos-systemd-etcs at master · Supervisor/initscripts

另外,参考:

Installing Supervisor and Superlance on CentOS

How to use supervisord in centos/fedora/red hat like systems

<code>curl https://raw.githubusercontent.com/Supervisor/initscripts/master/redhat-init-mingalevme &gt; /etc/rc.d/init.d/supervisord
</code>

python – How to automatically start supervisord on Linux (Ubuntu) – Server Fault

-》

https://gist.github.com/danmackinlay/176149

“an init.d script for supervisord”

大概知道了:

对于保存配置到:

/etc/rc.d/init.d/supervisord

中的,是为了支持:

serverice去管理supervisor的

而此处暂时不用去考虑,只考虑用systemctl去管理supervisor即可。

继续参考:

https://github.com/zokeber/supervisor-systemd

https://gist.github.com/mozillazg/6cbdcccbf46fe96a4edd

去试试

其中:

https://github.com/Supervisor/initscripts

-》

https://github.com/Supervisor/initscripts/blob/master/centos-systemd-etcs

只是上述完整步骤中,相对最核心的supervisord.service的配置而已

而虽然

https://github.com/zokeber/supervisor-systemd

写的很完整,但是配置文件的路径写的是:

/etc/supervisor/supervisord.conf

此处打算换成自己之前用的,且文件还存在的:

/etc/supervisord.conf

(以及相关的:

/etc/supervisord.d/supervisord.conf)

现在看起来就简单了:

自己本身此处已经存在了

/etc/supervisord.conf

/etc/supervisord.d/supervisord.conf

然后只要弄个:

supervisord.service

即可。

此处先确定pip安装后,存在supervisord和supervisorctl:

<code>[root@xxx-general-01 robotDemo]# ll /usr/bin/supervisor*
-rwxr-xr-x 1 root root 403 May 24 15:33 /usr/bin/supervisorctl
-rwxr-xr-x 1 root root 399 May 24 15:33 /usr/bin/supervisord
</code>

同时贴出之前:

【已解决】CentOS中用supervisor去管理gunicorn的Flask的app

(yum install supervisor)的supervisord.service供参考:

<code>[root@xx-general-01 robotDemo]# cat /usr/lib/systemd/system/supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf

[Install]
WantedBy=multi-user.target
</code>

然后去弄个supervisord.service

<code># supervisord service for systemd (CentOS 7.0+)

[Unit]
Description=supervisord - Supervisor process control system for UNIX
Documentation=http://supervisord.org
After=network.target

[Service]
Type=forking
# User=root
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
# RestartSec=50s
RestartSec=42s

[Install]
WantedBy=multi-user.target
</code>

放到:/usr/lib/systemd/system/ 中:

<code>[root@xx-general-01 system]# pwd
/usr/lib/systemd/system
[root@xx-general-01 system]# cat supervisord.service 
# supervisord service for systemd (CentOS 7.0+)

[Unit]
Description=supervisord - Supervisor process control system for UNIX
Documentation=http://supervisord.org
After=network.target

[Service]
Type=forking
# User=root
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
# RestartSec=50s
RestartSec=42s

[Install]
WantedBy=multi-user.target
</code>

然后reload后再start,结果出错:

【已解决】supervisor启动出错:Job for supervisord.service failed because the control process exited with error code

和:

【未解决】supervisor的conf配置中无法使用自定义变量或环境变量

【总结】

此处,想要在pip或easy_install安装supervisor

由于不是通过系统的包管理器,比如

CentOS的yum,去安装的,则默认是没有开启启动,没法通过systemctl或service去管理supervisord的,所以需要去加上这方面的配置,允许用通过systemctl或service去管理supervisord

具体做法是:

1.此处自己之前已经有了supervisor的配置

<code>[root@xx-general-01 robotDemo]# cat /etc/supervisord.conf 
; Sample supervisor config file.

[unix_http_server]
file=/var/run/supervisor/supervisor.sock   ; (the path to the socket file)
;chmod=0700                 ; sockef 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)
;username=user              ; (default is no username (open server))
;password=123               ; (default is no password (open server))

[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)
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)
environment=ROBOTDEMO_PRJOECT_ROOT=/xx/robotDemo,ROBOTDEMO_VIRTUALENV_ROOT=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG

; 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:///var/run/supervisor/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

; 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              ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=true              ; retstart at unexpected quit (default: true)
;startsecs=10                  ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;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_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    ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=unexpected        ; restart at unexpected quit (default: unexpected)
;startsecs=10                  ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;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  ; each refers to 'x' in [program:x] definitions
;priority=999                  ; the relative start priority (default 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 = supervisord.d/*.ini
files = /etc/supervisord.d/*.conf
</code>

和:

<code>[root@xx-general-01 robotDemo]# cat /etc/supervisord.d/supervisord_server.conf 
;ROBOTDEMO_PRJOECT_ROOT=/xx/robotDemo
;ROBOTDEMO_VIRTUALENV_ROOT=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG

[program:redis]
directory=%(ROBOTDEMO_PRJOECT_ROOT)s
command=/usr/bin/redis-server

autostart=true
autorestart=true

stdout_logfile=%(ROBOTDEMO_PRJOECT_ROOT)s/logs/redis-%(program_name)s-stdout.log
stdout_logfile_maxbytes=2MB
stdout_logfile_backups=10

stderr_logfile=%(ROBOTDEMO_PRJOECT_ROOT)s/logs/redis-%(program_name)s-stderr.log
stderr_logfile_maxbytes=2MB
stderr_logfile_backups=10


[program:robotDemo_CeleryWorker]
command=%(ROBOTDEMO_VIRTUALENV_ROOT)s/bin/celery worker -A app.celeryApp
directory=%(ROBOTDEMO_PRJOECT_ROOT)s
autostart=true
autorestart=true

stdout_logfile=%(ROBOTDEMO_PRJOECT_ROOT)s/logs/celery-worker-%(program_name)s-stdout.log
stdout_logfile_maxbytes=2MB
stdout_logfile_backups=10

stderr_logfile=%(ROBOTDEMO_PRJOECT_ROOT)s/logs/celery-worker-%(program_name)s-stderr.log
stderr_logfile_maxbytes=2MB
stderr_logfile_backups=10


[program:robotDemo_CeleryBeat]
command=%(ROBOTDEMO_VIRTUALENV_ROOT)s/bin/celery beat -A app.celeryApp -s %(ROBOTDEMO_PRJOECT_ROOT)s/runtime/celerybeat-schedule
directory=%(ROBOTDEMO_PRJOECT_ROOT)s
autostart=true
autorestart=true

stdout_logfile=%(ROBOTDEMO_PRJOECT_ROOT)s/logs/celery-beat-%(program_name)s-stdout.log
stdout_logfile_maxbytes=2MB
stdout_logfile_backups=10

stderr_logfile=%(ROBOTDEMO_PRJOECT_ROOT)s/logs/celery-beat-%(program_name)s-stderr.log
stderr_logfile_maxbytes=2MB
stderr_logfile_backups=10


[program:robotDemo]
command=%(ROBOTDEMO_VIRTUALENV_ROOT)s/bin/gunicorn -c gunicorn_config.py app:app
directory=%(ROBOTDEMO_PRJOECT_ROOT)s
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
killasgroup=true
stopasgroup=true

stdout_logfile=%(ROBOTDEMO_PRJOECT_ROOT)s/logs/supervisord-%(program_name)s-stdout.log
stdout_logfile_maxbytes=2MB
stdout_logfile_backups=10

stderr_logfile=%(ROBOTDEMO_PRJOECT_ROOT)s/logs/supervisord-%(program_name)s-stderr.log
stderr_logfile_maxbytes=2MB
stderr_logfile_backups=10
[root@xx-general-01 robotDemo]# 
</code>

注意:

此处忽略掉environment部分,待后续解决。

然后去添加,用于systemctl控制supervisor的配置文件

<code>[root@xx-general-01 robotDemo]# cat /usr/lib/systemd/system/supervisord.service 
# supervisord service for systemd (CentOS 7.0+)

[Unit]
Description=supervisord - Supervisor process control system for UNIX
Documentation=http://supervisord.org
After=network.target

[Service]
Type=forking
# User=root
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
# RestartSec=50s
RestartSec=42s

[Install]
WantedBy=multi-user.target
</code>

然后记得先要reload使得配置生效:

<code>systemctl daemon-reload
</code>

然后就可以正常去启动:

<code>systemctl start supervisord
</code>

查看状态:

<code>systemctl status supervisord
</code>

设置开机启动:

<code>systemctl enable supervisord
</code>

了。

转载请注明:在路上 » 【已解决】CentOS中如何用systemctl管理supervisor包括设置supervisor开机启动

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.205 seconds, using 22.21MB memory