我在我的supervisord.conf中有[program:A]
和[program:B]
B
依赖于A
,也就是说:
A
应该在B
之前启动。
如何通过supervisor来确保这一点?
我在我的supervisord.conf中有[program:A]
和[program:B]
B
依赖于A
,也就是说:
A
应该在B
之前启动。
如何通过supervisor来确保这一点?
supervisord
不直接支持依赖关系。你的选项是:
使用优先级。将 A
的 priority
设置为一个较低的值,它会在 B
之前启动,并在 B
之后关闭。 priority
的默认值为 999
。
如果你将这两个程序放在同一组中,那么你可以同时启动和停止它们,通过优先级来调节它们的启动和停止顺序。
编写一个事件监听器来监听 A
的 PROCESS_STATE
从 STARTING
到 RUNNING
的转换以及 STOPPING
事件,然后根据这些事件指示 supervisord
启动和停止 B
。让 A
自动启动,但对于 B
禁用自动启动,以便事件处理程序控制它。
startsecs
参数)。相反,它会立即在A之后启动B,这在许多情况下都不是你想要的(足够的)。这种行为已经在2012年被报告过,但截至2014年仍未得到修复。 - miguno如果你想捷径,跳过关于事件监听器的文档阅读和跳过修改程序使其理解事件的步骤,则:
你可以启动一个 Bash 脚本代替直接启动依赖于 A
的程序 B
,该脚本会休眠直到 A
已启动,然后再启动 B
。例如,如果你有一个 PostgreSQL 数据库和一个在 PostgreSQL 启动之前不应该启动的服务器:
[program:server]
autorestart=true
command=/.../start-server.sh
[program:postgres]
user=postgres
autorestart=true
command=/usr/lib/postgresql/9.3/bin/postgres ...
然后在 start-server.sh
文件内:
#!/bin/bash
# Wait until PostgreSQL started and listens on port 5432.
while [ -z "`netstat -tln | grep 5432`" ]; do
echo 'Waiting for PostgreSQL to start ...'
sleep 1
done
echo 'PostgreSQL started.'
# Start server.
echo 'Starting server...'
/.../really-start-the-server
supervisorctl start B
。supervisor.cfg
文件:[supervisord]
nodaemon=false
pidfile=/tmp/supervisor.pid
logfile=/logs/supervisor.log
[unix_http_server]
file=/var/run/supervisor.sock
[rpcinterface:supervisor]
supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock
[program:A]
command=do_a
[program:B]
command=do_b
autostart=false
do_a
程序包含以下内容:
#!/bin/bash
#do things
supervisorctl start B
说实话,这是@drrzmr建议的解决方案,但我当时并没有理解它。
autorestart=false
。对于某些应用程序来说还不错,但对于一些其他应用程序(例如数据库)来说并不太好。 - Charlie Reitzel这个 解决方案对我来说非常好用!
一个解决方法是在进程上设置
autostart=false
,然后创建一个引导脚本,其中autostart=true
和autorestart=false
(仅一次)。引导程序可以是调用的 shell 脚本 的supervisorctl start
,为每个进程启动。supervisorctl start
将阻塞,直到进程成功启动。