简体   繁体   English

动态的cron工作?

[英]Dynamic cron jobs?

I'm writing a script that needs to be called at a random time during the day, but am not sure how to accomplish this. 我正在编写一个脚本,该脚本需要在一天中的随机时间调用,但是不确定如何实现。

I don't want to waste server resources to run a cron job every minute. 我不想浪费服务器资源来每分钟运行一次cron作业。

I want the script to be called at random, so generating the random times for say a month in advance and then creating cron jobs for each of them isn't what I'm looking for. 我希望脚本被随机调用,因此,我需要提前一个月随机生成一个月的时间,然后为每个脚本创建cron作业。

Also this script only needs to be executed once a day. 同样,该脚本每天只需要执行一次。

Thanks in advance! 提前致谢!

Have each run submit a further run using 'at' with a random time? 是否每次运行都使用随机的“ at”提交进一步的运行? That wouldn't guarantee it ran every day, but you could get that as an average. 那不能保证它每天都在运行,但是您可以平均得到它。

This will add a cron job: 这将添加一个cron作业:

echo "* * * * * /path/to/script" | crontab

You need to generate a proper time instead of * * * * * (it will run every minute otherwise). 您需要生成一个适当的时间而不是* * * * * (否则它将每分钟运行一次)。

echo "* * * * * /path/to/script" | crontab -r

will remove the job. 将删除工作。 So, now you need to call those from PHP script, I don't remember exactly how it's done in PHP. 因此,现在您需要从PHP脚本中调用这些脚本,我不记得它是如何在PHP中完成的。

EDIT 编辑

http://php.net/manual/en/function.system.php http://php.net/manual/en/function.system.php

MORE EDIT 更多编辑

Every time script executes, it's two last operations might be removing the current cron job and generating another one for the next day. 每次执行脚本时,最后两个操作可能是删除当前的cron作业,并在第二天生成另一个作业。

EVEN MORE EDIT 甚至更多的编辑

http://codepad.org/Z1B2v4lF -- here's how I would do it in Python. http://codepad.org/Z1B2v4lF-这是我在Python中的处理方式。 This may run more than once a day. 每天可能运行不止一次。 If running only once a day is a must, you can add a day-of-month value as well. 如果必须每天仅运行一次,则还可以添加一个“每月”值。

如果可以访问它,请在目标脚本运行时修改crontab文件并随机添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM