使用主管作为CRON

使用主管作为CRON,cron,supervisord,Cron,Supervisord,有没有办法配置为每X秒运行一次命令(比如CRON) 我看到了eventlistener和勾选事件的示例 [eventlistener:memmon] command=memmon -a 200MB -m bob@example.com events=TICK_60 但是它只运行一次命令。问题 正如您在memmon示例中看到的,supervisord没有执行memmon-A200MB-mbob@example.com在每次活动中。相反,它只启动此事件侦听器一次(如果配置池,则可能启动几次),然后

有没有办法配置为每X秒运行一次命令(比如CRON)

我看到了eventlistener和勾选事件的示例

[eventlistener:memmon]
command=memmon -a 200MB -m bob@example.com
events=TICK_60
但是它只运行一次命令。

问题 正如您在memmon示例中看到的,supervisord没有执行
memmon-A200MB-mbob@example.com
在每次活动中。相反,它只启动此事件侦听器一次(如果配置池,则可能启动几次),然后通过现有进程的标准输入发送每个新事件

解决方案 因此,对于要在事件中触发的每种附加类型的功能,您确实需要找到或编写一个与主管兼容的事件侦听器

示例实现方法 设置配置并编写侦听器 编写SupervisorORD.cfg事件部分

[eventlistener:passthru]
command=/tmp/simple.py /bin/date -u +"%%s %%S:%%H:%%d:%%m"
events=TICK_60
(注:逃逸)

编写一个简单的.py事件侦听器

通过对进行更改创建此simple.py侦听器,使其使用任何剩余参数执行其第一个参数:

#/usr/bin/python
导入系统
导入子流程
def写入数据:
系统标准输出写入
sys.stdout.flush()
def写入标准:
系统标准写入
sys.stderr.flush()
def主(args):
而1:
写入_stdout('READY\n')#从确认到准备的转换
line=sys.stdin.readline()#从stdin读取标题行
写入(行)#打印到stderr
headers=dict([x.split(':'),用于第x行中的x.split()
data=sys.stdin.read(int(headers['len'])#读取事件负载
res=subprocess.call(args,stdout=sys.stderr);#不要弄乱真正的标准
写入标准(数据)
写入#标准输出('RESULT 2\nOK')#从就绪转换为已确认
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main(sys.argv[1:])
导入系统
确保管理器配置工作正常 (chmod+x…)

在supervisord.cfg中更改侦听器的参数

[eventlistener:passthru]
command=/tmp/simple.py /tmp/hiworld.php
;stdout_logfile=/tmp/passthru 
events=TICK_60
;autorestart=true
;startsecs=0
重新加载命令和测试 (重读似乎没有检测到此更改)

终点
现在,所需的命令每60秒运行一次。您现在可以阅读以调整权限、位置、日志等的详细信息。

主管不容易支持此操作

但为了实现您的目标,您可以使用supervisor启动cron(例如docker容器):

在docker中安装
cron
apt get Install cron
,使用类似debian的docker)

在管理器配置中:

[program:cron]
command=cron -f -L 15
autostart=true
autorestart=true
-f
用于前台,
-l15
将输出所有cron日志


然后使用用户crontab、全局
/etc/crontab
或任何crontab特殊目录(
/etc/cron.hourly
/etc/cron.daily
,…)

为什么发明轮子?您可以同时使用
cron
supervisorORD

在supervisord中,使用
autostart=false创建任务


在cron中,使用
***supervisorctl start
每分钟启动一次任务

您可以使用crobtab来管理和安排您的主管程序

使用命令
supervisorctl start


注意:这将只启动一个supervisor程序实例。如果它已经在运行并且crontab试图再次触发它,
supervisortl start
命令将不会启动新实例。

您可以调用bash
sleep
命令:

[program:mycmd]
command=bash -c 'sleep 300 && exec <your command here>'
autorestart=true
[程序:mycmd]
command=bash-c'sleep 300&&exec'
自动重新启动=真

这将每5分钟运行一次您的命令。不要忘记使用命令替换bash进程的
exec
part,这样主管将获得正确的退出代码。

我尝试了
command=python/home/supervisor.py“php-f/home/test.php”
,但它不起作用。我的意思是
supervisorctl
说它正在运行,但是从来没有调用过
test.php
。您能提供一些工作示例吗?在我的示例中,我会更改:command=/tmp/simple.py/bin/date=>command=/tmp/simple.py/home/test.php和put#!//php位于test.php的顶部。但是删除引号和使用php的完整路径可能也会起作用,它可以很好地处理命令及其参数,其中命令是第一个参数。正如我在上一篇评论中所说的,您试图引用一个命令及其参数会导致一个参数不是可执行文件,如果您坚持使用引号,则必须使用shell将其展开
command=listener mycommand arg1 arg2
command=listener/bin/sh-c“mycommand arg1 arg2”
有意义,
command=listener“mycommand arg1 arg2”
没有意义。我仍然无法使它与包含参数的命令一起工作。你能用一些包含参数的命令更新你的答案吗?@barbushin,到目前为止,我添加了一些参数,显示了我能想到的唯一逃避问题。答案是否定的,supervisord不能运行像cron这样的作业。事件不能满足您的要求。Alex,按照您的解决方案,我们如何管理任务运行计划?这已经是近一年前答案的一部分-您是否还想添加其他内容?是的,cron在任何给定时间只能运行一个supervisor程序实例的部分。即使在supervisor程序仍在运行时触发了cron,cron也无法启动新实例。StackOverflow要求在该站点上显示答案,而在外部站点上显示为否。我高估了你的答案,但对其他人,我编辑了你的答案这是对docker container和Supervisor最适合我的答案可能是simples T解决方案,如果cronjob,但你错过了自动重新启动选项或while循环来再次运行它!所以:选项A)[program:cleanup]command=bash-c'date>/var/log/prueba.log&&sleep 5'autorestart=true选项B)command=bash-c'while true;do date>>/var/log/prueba.log;睡眠5;done'autorestart=true
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> 
[program:cron]
command=cron -f -L 15
autostart=true
autorestart=true
[program:mycmd]
command=bash -c 'sleep 300 && exec <your command here>'
autorestart=true