有没有办法配置supervisor每X秒运行一些命令(如CRON)?
我看到了eventlistener和TICK_事件的例子
[eventlistener:memmon]
command=memmon -a 200MB -m bob@example.com
events=TICK_60
Run Code Online (Sandbox Code Playgroud)
但它只运行一次命令.
los*_*der 20
正如您在memmon示例中看到的那样,supervisord并未memmon -a 200MB -m bob@example.com在每个事件中执行.相反,它启动此事件侦听器一次(或者如果配置池,可能会启动几次),然后通过现有进程的标准输入发送每个新事件.
因此,您确实需要为要在事件上触发的每种其他类型的功能查找或编写与Supervisor兼容的事件侦听器.
写一个supervisord.cfg事件部分
[eventlistener:passthru]
command=/tmp/simple.py /bin/date -u +"%%s %%S:%%H:%%d:%%m"
events=TICK_60
Run Code Online (Sandbox Code Playgroud)
(注意 - 为configParser转义%)
编写一个simple.py事件监听器
通过从文档中更改示例侦听器来创建此simple.py侦听器,以便它使用任何剩余的参数执行其第一个参数:
#! /usr/bin/python
import sys
import subprocess
def write_stdout(s):
sys.stdout.write(s)
sys.stdout.flush()
def write_stderr(s):
sys.stderr.write(s)
sys.stderr.flush()
def main(args):
while 1:
write_stdout('READY\n') # transition from ACKNOWLEDGED to READY
line = sys.stdin.readline() # read header line from stdin
write_stderr(line) # print it out to stderr
headers = dict([ x.split(':') for x in line.split() ])
data = sys.stdin.read(int(headers['len'])) # read the event payload
res = subprocess.call(args, stdout=sys.stderr); # don't mess with real stdout
write_stderr(data)
write_stdout('RESULT 2\nOK') # transition from READY to ACKNOWLEDGED
if __name__ == '__main__':
main(sys.argv[1:])
import sys
Run Code Online (Sandbox Code Playgroud)
$ supervisorctl [-c cfg]
supervisor> status
passthru RUNNING pid 4471, uptime 0:00:32
supervisor> tail passthru
OKREADY
RESULT 2
OKREADY
...
supervisor> tail passthru stderr
supervisor> tail passthru stderr
ver:3.0 server:supervisor serial:0 pool:passthru poolserial:0 eventname:TICK_60 len:15
1451411161 01:17:29:12 <--- output
when:1451411160ver:3.0 server:supervisor serial:1 pool:passthru poolserial:1 eventname:TICK_60 len:15
1451411220 00:17:29:12 <--- output
when:1451411220
Run Code Online (Sandbox Code Playgroud)
现在date -u +"%s %S:%H:%d:%m"每60秒运行一次.
创建可执行脚本
/tmp/hiworld.php:
#! /usr/bin/php
<?= "hiya\n";
Run Code Online (Sandbox Code Playgroud)
(chmod + x ......)
在supervisord.cfg中更改监听器的参数
[eventlistener:passthru]
command=/tmp/simple.py /tmp/hiworld.php
;stdout_logfile=/tmp/passthru
events=TICK_60
;autorestart=true
;startsecs=0
Run Code Online (Sandbox Code Playgroud)
重新加载supervisord和测试 (重读似乎没有检测到这种变化)
supervisor> reload
Really restart the remote supervisord process y/N? y
Restarted supervisord
supervisor> status
passthru RUNNING pid 6017, uptime 0:00:10
supervisor> tail passthru stderr
supervisor> status
passthru RUNNING pid 6017, uptime 0:00:21
supervisor> status
passthru RUNNING pid 6017, uptime 0:01:01
supervisor> tail passthru stderr
ver:3.0 server:supervisor serial:316 pool:passthru poolserial:0 eventname:TICK_60 len:15
hiya
when:1418926740
supervisor>
Run Code Online (Sandbox Code Playgroud)
现在所需的命令每60秒运行一次.您现在阅读以调整权限,位置,日志等的详细信息.
Ale*_*exM 13
为什么要发明轮子?您可以使用cron和supervisord在一起.
在supervisord中,创建一个任务 autostart=false
在cron中,用于* * * * * supervisorctl start <taskname>每分钟启动任务
mix*_*xel 11
您可以调用 bashsleep命令:
[program:mycmd]
command=bash -c 'sleep 300 && exec <your command here>'
autorestart=true
Run Code Online (Sandbox Code Playgroud)
这将每 5 分钟运行一次您的命令。不要忘记exec用您的命令替换 bash 进程的一部分,以便主管将获得正确的退出代码。
主管不容易支持.
但要实现您的目标,您可以使用supervisor启动cron(例如,对于docker容器):
https://gist.github.com/martinrusev/7015e393d46647dbad15