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

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

supervisor crifan 6817浏览 0评论

折腾:

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

期间,剩下的问题就是去解决:

如何在supervisor中的conf配置文件中,实现自定义变量,实现自己的

项目根目录等一次性定义,而后续直接引用,无需写多次的问题了。

然后去折腾试试

<code>[root@xxx-general-01 system]# journalctl -xe
May 24 15:55:04 xx-general-01 systemd[1]: supervisord.service failed.
May 24 15:55:04 xx-general-01 polkitd[505]: Unregistered Authentication Agent for unix-process:24072:86119705 (system bus name :1.3613, object path /org/freedesktop/PolicyKit1/Aut
May 24 15:55:46 xx-general-01 systemd[1]: supervisord.service holdoff time over, scheduling restart.
May 24 15:55:46 xx-general-01 systemd[1]: Starting supervisord - Supervisor process control system for UNIX...
-- Subject: Unit supervisord.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit supervisord.service has begun starting up.
May 24 15:55:47 xx-general-01 supervisord[24082]: Error: could not find config file /etc/supervisord.conf
May 24 15:55:47 xx-general-01 supervisord[24082]: For help, use /usr/bin/supervisord -h
May 24 15:55:47 xx-general-01 systemd[1]: supervisord.service: control process exited, code=exited status=2
May 24 15:55:47 xx-general-01 systemd[1]: Failed to start supervisord - Supervisor process control system for UNIX.
-- Subject: Unit supervisord.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit supervisord.service has failed.
-- 
-- The result is failed.
May 24 15:55:47 x-general-01 systemd[1]: Unit supervisord.service entered failed state.
May 24 15:55:47 xx-general-01 systemd[1]: supervisord.service failed.
May 24 15:56:26 xx-general-01 polkitd[505]: Registered Authentication Agent for unix-process:24085:86127892 (system bus name :1.3614 [/usr/bin/pkttyagent --notify-fd 5 --fallback]
May 24 15:56:29 xx-general-01 systemd[1]: supervisord.service holdoff time over, scheduling restart.
May 24 15:56:29 xx-general-01 systemd[1]: Starting supervisord - Supervisor process control system for UNIX...
-- Subject: Unit supervisord.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit supervisord.service has begun starting up.
May 24 15:56:29 xx-general-01 supervisord[24091]: Error: Format string '%(ENV_ROBOTDEMO_PRJOECT_ROOT)s' for 'program:redis.directory' contains names ('ENV_ROBOTDEMO_PRJOECT_ROOT')
May 24 15:56:29 xx-general-01 supervisord[24091]: For help, use /usr/bin/supervisord -h
May 24 15:56:29 xx-general-01 systemd[1]: supervisord.service: control process exited, code=exited status=2
May 24 15:56:29 xx-general-01 systemd[1]: Failed to start supervisord - Supervisor process control system for UNIX.
-- Subject: Unit supervisord.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit supervisord.service has failed.
-- 
-- The result is failed.
May 24 15:56:29 xx-general-01 systemd[1]: Unit supervisord.service entered failed state.
May 24 15:56:29 xx-general-01 systemd[1]: supervisord.service failed.
May 24 15:56:29 xx-general-01 polkitd[505]: Unregistered Authentication Agent for unix-process:24085:86127892 (system bus name :1.3614, object path /org/freedesktop/PolicyKit1/Aut
[root@xx-general-01 system]# tail /var/log/supervisor/supervisord.log
</code>

可以看出:

Error: could not find config file /etc/supervisord.conf

是前面的错误,此处已经解决了。

而最新的错误是:

<code>May 24 15:56:29 xx-general-01 supervisord[24091]: Error: Format string '%(ENV_ROBOTDEMO_PRJOECT_ROOT)s' for 'program:redis.directory' contains names ('ENV_ROBOTDEMO_PRJOECT_ROOT')
</code>

很明显是配置文件中,之前为了测试环境变量,而(把正确的)改错了的

<code>[program:redis]
;directory=%(ROBOTDEMO_PRJOECT_ROOT)s
directory=%(ENV_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=%(ENV_ROBOTDEMO_PRJOECT_ROOT)s/logs/redis-%(program_name)s-stdout.log
</code>

所以去改回来

<code>[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
</code>

结果:

<code>[root@xx-general-01 robotDemo]# systemctl start supervisord             
</code>

半天没有返回结果,很奇怪啊

结果还是出错:

<code>[root@xx-general-01 robotDemo]# systemctl start supervisord             
Job for supervisord.service failed because the control process exited with error code. See "systemctl status supervisord.service" and "journalctl -xe" for details.
</code>

再去看看原因

<code>[root@xx-general-01 robotDemo]# systemctl status supervisord.service
● supervisord.service - supervisord - Supervisor process control system for UNIX
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
   Active: activating (auto-restart) (Result: exit-code) since Thu 2018-05-24 16:05:38 CST; 29s ago
     Docs: http://supervisord.org
  Process: 24190 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=2)
 Main PID: 678 (code=exited, status=2)

May 24 16:05:38 xx-general-01 systemd[1]: Failed to start supervisord - Supervisor process control system for UNIX.
May 24 16:05:38 xx-general-01 systemd[1]: Unit supervisord.service entered failed state.
May 24 16:05:38 xx-general-01 systemd[1]: supervisord.service failed.
[root@xx-general-01 robotDemo]# journalctl -xe
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit supervisord.service has failed.
-- 
-- The result is failed.
May 24 16:09:09 xx-general-01 systemd[1]: Unit supervisord.service entered failed state.
May 24 16:09:09 xx-general-01 systemd[1]: supervisord.service failed.
May 24 16:09:51 xx-general-01 systemd[1]: supervisord.service holdoff time over, scheduling restart.
May 24 16:09:51 xx-general-01 systemd[1]: Starting supervisord - Supervisor process control system for UNIX...
-- Subject: Unit supervisord.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit supervisord.service has begun starting up.
May 24 16:09:52 xx-general-01 supervisord[24228]: Error: Format string '%(ROBOTDEMO_PRJOECT_ROOT)s' for 'program:redis.directory' contains names ('ROBOTDEMO_PRJOECT_ROOT') whi
May 24 16:09:52 xx-general-01 supervisord[24228]: For help, use /usr/bin/supervisord -h
May 24 16:09:52 xx-general-01 systemd[1]: supervisord.service: control process exited, code=exited status=2
May 24 16:09:52 xx-general-01 systemd[1]: Failed to start supervisord - Supervisor process control system for UNIX.
-- Subject: Unit supervisord.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit supervisord.service has failed.
-- 
-- The result is failed.
May 24 16:09:52 xx-general-01 systemd[1]: Unit supervisord.service entered failed state.
May 24 16:09:52 xx-general-01 systemd[1]: supervisord.service failed.
May 24 16:10:01 xx-general-01 systemd[1]: Started Session 1802 of user root.
-- Subject: Unit session-1802.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit session-1802.scope has finished starting up.
-- 
-- The start-up result is done.
May 24 16:10:01 xx-general-01 systemd[1]: Starting Session 1802 of user root.
-- Subject: Unit session-1802.scope has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit session-1802.scope has begun starting up.
May 24 16:10:01 xx-general-01 CROND[24233]: (root) CMD (/usr/lib64/sa/sa1 1 1)
</code>

还是无法识别环境变量

难道是之前猜测的:

放在:

/etc/supervisord.conf

中的:

<code>[supervisord]
...
environment=ROBOTDEMO_PRJOECT_ROOT=/xx/robotDemo,ROBOTDEMO_VIRTUALENV_ROOT=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG
</code>

环境变量,无法传递到:

/etc/supervisord.d/supervisord_server.conf

中的:

[program:xxx]

感觉问题变成了:

supervisor的字配置文件中,无法识别前面supervisord中定义的environment

supervisor environment sub config

[supervisord] environment not picked up · Issue #91 · Supervisor/supervisor

“This environment is not applied to child processes.”

python – How to set environment variables in Supervisor service – Stack Overflow

Configuration File — Supervisor 3.3.4 documentation

Environment Variables

“In Supervisor 3.2 and later, %(ENV_X)s expressions are supported in all options. In prior versions, some options support them, but most do not. See the documentation for each option below.”

感觉意思是:

3.2版本之前,%(xxx)s不是所有的option都支持:好像就只是supervisord的部分支持%(xxx)s

http://supervisord.org/configuration.html#program-x-section-example

“[program:cat]

command=/bin/cat

environment=A=”1″,B=”2””

说明program部分,也是支持environment的

那难道我此处需要给每个program部分都加上定义?

那就太冗余了

去试试,在顶部加上试试

结果:

<code>environment=ROBOTDEMO_PRJOECT_ROOT=/xx/robotDemo,ROBOTDEMO_VIRTUALENV_ROOT=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG
</code>

出错:

<code>May 24 16:29:35 xx-general-01 supervisord[24388]: Error: File contains no section headers.
May 24 16:29:35 xx-general-01 supervisord[24388]: file: /etc/supervisord.d/supervisord_server.conf, line: 3
May 24 16:29:35 xx-general-01 supervisord[24388]: 'environment=ROBOTDEMO_PRJOECT_ROOT=/xx/robotDemo,ROBOTDEMO_VIRTUALENV_ROOT=/root/.local/shar
May 24 16:29:35 xx-general-01 supervisord[24388]:
</code>

加上个头试试

<code>[supervisord]
environment=ROBOTDEMO_PRJOECT_ROOT=/xx/robotDemo,ROBOTDEMO_VIRTUALENV_ROOT=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG
</code>

问题依旧

<code>May 24 16:31:41 xx-general-01 supervisord[24420]: Error: Format string '%(ROBOTDEMO_PRJOECT_ROOT)s' for 'program:redis.directory' contains names ('ROBOTDEMO_PRJOECT_ROOT') whi
</code>

看到官网

http://supervisord.org/configuration.html#program-x-section-example

解释:

<code>environment

A list of key/value pairs in the form KEY="val",KEY2="val2" that will be placed in the supervisordprocess’ environment (and as a result in all of its child process’ environments). This option can include the value %(here)s, which expands to the directory in which the supervisord configuration file was found. Values containing non-alphanumeric characters should be quoted (e.g. KEY="val:123",KEY2="val,456"). Otherwise, quoting the values is optional but recommended. To escape percent characters, simply use two. (e.g. URI="/first%%20name") Note that subprocesses will inherit the environment variables of the shell used to start supervisord except for the ones overridden here and within the program’s environment option. See Subprocess Environment.
Default: no values
Required: No.
Introduced: 3.0
</code>

算了,还是给每个section,都加上对应的定义吧

<code>[program:redis]
environment=ROBOTDEMO_PRJOECT_ROOT=/xx/robotDemo,ROBOTDEMO_VIRTUALENV_ROOT=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG
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]
environment=ROBOTDEMO_PRJOECT_ROOT=/xxx/robotDemo,ROBOTDEMO_VIRTUALENV_ROOT=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG
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]
environment=ROBOTDEMO_PRJOECT_ROOT=/xx/robotDemo,ROBOTDEMO_VIRTUALENV_ROOT=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG
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]
environment=ROBOTDEMO_PRJOECT_ROOT=/xx/robotDemo,ROBOTDEMO_VIRTUALENV_ROOT=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG
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

</code>

结果:

竟然还是同样错误:

<code>May 24 16:40:51 xx-general-01 supervisord[24498]: Error: Format string '%(ROBOTDEMO_PRJOECT_ROOT)s' for 'program:redis.directory' contains names ('ROBOTDEMO_PRJOECT_ROOT') whi
May 24 16:40:51 xx-general-01 supervisord[24498]: For help, use /usr/bin/supervisord -h
</code>

看:

https://stackoverflow.com/questions/17055951/how-to-set-environment-variables-in-supervisor-service

感觉是:

此处定义了environment,不是给此处用的??

supervisor Error: Format string  or  contains names

找不到合适的解释和方案

感觉是:

environment中,只是用来:

  • 引用来自外部的环境变量的值

  • 或者override environment的值,用于child process使用

而不能:

  • 在conf中定义一个变量,用于config中自己引用

所以现在只能考虑:

在外部定义环境变量,传递进去了:

supervisor pass environment

python – How to set environment variables in Supervisor service – Stack Overflow

supervisor 传递环境变量

supervisor设置环境变量 – CSDN博客

python – 如何在Supervisor服务中设置环境变量 – 代码日志

supervisor 加载系统环境变量问题 – V2EX

Supervisor 配置nodejs的环境变量 – klk2

supervisor配置环境变量(PATH) – imzye – 博客园

supervisor设定环境变量 – Linux运维 – 运维网 – iyunv.com

然后试了试:

<code>export ROBOTDEMO_PRJOECT_ROOT=/xx/robotDemo
export ENV_ROBOTDEMO_PRJOECT_ROOT=/xx/robotDemo

[root@xx-general-01 robotDemo]# echo $ROBOTDEMO_PRJOECT_ROOT 
/xx/robotDemo
[root@xx-general-01 robotDemo]# echo $ENV_ROBOTDEMO_PRJOECT_ROOT 
/xx/robotDemo
</code>

确保:

ROBOTDEMO_PRJOECT_ROOT

ENV_ROBOTDEMO_PRJOECT_ROOT

都环境变量了。

结果去运行:

systemctl start supervisord

都还是出错:

<code> Error: Format string '%(ENV_ROBOTDEMO_PRJOECT_ROOT)s' for 'program:redis.directory' contains names
</code>

真是奇了怪了。

总之是:

supervisor中conf的配置中,没法包含当前自己定义的变量

supervisor config contain own variable

【总结】

所以,最终是

supervisor不支持在conf的配置中,想要包含当前自己定义的变量

只能放弃。

且此处试了环境变量设置了值,不知道为何conf中也无法引用到。

且即使可以引用,也不是我想要的。所以放弃。

转载请注明:在路上 » 【未解决】supervisor的conf配置中无法使用自定义变量或环境变量

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (2)

  1. 我也遇到这个问题了。。。conf中引用自定义的%(ENV_name)s。。如果使用/usr/bin/supervisord -c /etc/supervisor/supervisord.conf启动是没问题的。。但是如果使用守护进程启动。systemctl start supervisord就会提示环境变量找不到。。不知道是哪的问题
    森林4年前 (2020-01-09)回复
  2. 可以试一试这个:https://codeday.me/bug/20190320/788527.html
    ls5年前 (2019-05-26)回复
85 queries in 0.166 seconds, using 22.16MB memory