如何每天运行我的脚本 x 次? (Linux 上的红宝石)
我想在我的linux机器上每天运行x次我的ruby脚本(数量可能会改变)。如果我不希望它同时发生,最好的方法是什么?我希望时间(小时和分钟)随机
我正在考虑使用at
命令。该脚本将在 x 小时/分钟左右由 at
调用,然后该脚本将由 at
设置另一个调用。不确定是否有更好的方法或只有红宝石的方法。
I want to run my ruby script x times a day (the number might change) on my linux box. What would be the best way to do so if I do not want it to happen at the same time? I want the time (hour and minute) to be random
I was thinking of using at
command. The script would be called by at
in x hours/minutes or so and then the script would set up another call by at
. Not sure if there is any better way or only ruby way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会考虑使用
at
程序来运行程序(而不是直接使用cron
,因为cron
实际上只按固定的时间表工作) 。我还会创建一个程序(我会使用 Perl;您将使用 Ruby)来安排随机延迟,直到下次执行作业为止。您需要考虑该作业在 24 小时内执行“x”次是否至关重要,以及随机性应如何发挥作用。时间变化范围是多少。例如,您可能有一个
cron
作业在午夜加 7 分钟运行,然后它会在一天中均匀间隔地安排“x”个at
作业,并具有随机偏差在 ±30 分钟的时间表中。或者您可能更喜欢一种替代方案,以 24/x 小时的平均间隔和一定量的随机偏差来安排作业。不同之处在于,第一个机制保证你在一天中获得 x 个事件(除非你把事情做得太极端);第二个有时可能在 24 小时内仅获得 x-1 个事件或 x+1 个事件。I'd consider using the
at
program to run the programs (instead of usingcron
directly, becausecron
really only works on a fixed schedule). I'd also create a program (I'd use Perl; you'll use Ruby) to schedule a random delay until the next time the job is executed.You'll need to consider whether it is crucial that the job is executed 'x' times in 24 hours, and how the randomness should work. What is the range of variation in times. For example, you might have a
cron
job run at midnight plus 7 minutes, say, which then schedules 'x'at
jobs spaced evenly through the day, with a random deviation in the schedule of ±30 minutes. Or you might prefer an alternative that schedules a the jobs with an average gap of 24/x hours and a random deviation of some amount. The difference is that the first mechanism guarantees that you get x events in the day (unless you make things too extreme); the second might sometimes only get x-1 events, or x+1 events, in 24 hours.我认为调度程序解决方案是有位限制的,为了获得最灵活的随机操作,请将脚本转换为守护进程并自己编写循环/等待。
对于 Ruby 来说似乎是这样的: http://raa.ruby-lang.org/project /守护进程/
I think scheduler solutions are bit limiting, to get most flexible random action, turn your script to daemon and code the loop / wait yourself.
For Ruby there seems to be this: http://raa.ruby-lang.org/project/daemons/
我想你可以设置一个 cronjob 来调用 bash 脚本,该脚本会随机延迟执行,但我不知道你是否可以在 cronjob 内以某种方式做到这一点。
您可以在此网站上找到有关如何执行此操作的一些信息,如果您不这样做的话如果不了解 crontab 和 cronjobs,您可以在此处找到更多相关信息。
I guess you can setup a cronjob that calls on a bash script which delays execution by a random time but I don't know if you can do it somehow inside the cronjob.
You can find some information on how to do that on this site and if you don't know about crontab and cronjobs you can find more information about that here.
如果您想每天运行 X 次,请将 crontab 条目设置为:
0 */X * * * command_to_run
其中 X 是您想要触发作业以获得每天所需执行次数的每小时间隔。例如,使用 2 每两小时触发一次,每天总共执行 12 次。
在你的代码中,在顶部使用它来强制它在该 cron 间隔之前随机休眠一段时间:
这个想法是 cron 将以固定的时间间隔启动你的程序,但随后它会在该间隔内休眠一些随机的秒数继续。
将 EXECUTION_TIME 的值更改为您认为代码运行所需的时间,以便让它有机会在下一个间隔发生之前完成。将 INTERVAL 中的“2”更改为您的 cron 间隔。
我还没有测试过这个,但它应该有效,或者至少让你走上正确的道路。
If you want to run X times a day, set your crontab entry to:
0 */X * * * command_to_run
where X is the hourly interval you want to fire your job on to get the desired number of executions/day. For instance, use 2 to fire off every two hours for a total of 12 executions/day.
In your code use this at the top to force it to sleep a random time up to that cron interval:
The idea is that cron will start your program at a regular interval, but then it will sleep some random number of seconds within that interval before continuing.
Change the value for EXECUTION_TIME to however long you think it will take for the code to run, to give it a chance to finish before the next interval occurs. Change the "2" in the INTERVAL to whatever your cron interval is.
I haven't tested this but it should work, or at least get you on the right path.