每105分钟运行cron [英] run cron every 105 minutes

查看:235
本文介绍了每105分钟运行cron的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要以15分钟的间隔通过cron运行7个不同的脚本。我已经确定每个脚本需要运行每105分钟(每1小时45分钟)如下:

 脚本1脚本2脚本3脚本4脚本5脚本6脚本7 
00:15 00:30 00:45 01:00 01:15 01:30 01:45
02:00 02:15 02: 30 02:45 03:00 03:15 03:30

等...



现在我知道我不能把这个:

  * / 105 * * * * something 

因为分钟只能达到59分。



除了单独写一行之外,是否有一个简单的方法来做到这一点?



任何帮助将非常感谢。

解决方案

位于



编写一个包装程序(或构建到脚本中):

  #!/ bin / bash 
at -f $ 0 now + 105分钟
< program>

然后在-f< script> < time> 。






或使用脚本生成crontab条目我的第一个草稿有点太长了,不能成为一个联机):

 #!/ usr / bin / perl -Ws 
use strict;
use DateTime;
our($ period,$ offset,$ command);
my($ offh,$ offm)= split(/:/,$ offset ||'0:00');
my%crontab =();
for(my $ i = 0; $ i <1440; $ i + = $ period){
my($ h,$ m)= split /:/,'DateTime' - > today-> set('hour',$ offh,'minute',$ offm) - > add('minutes',$ i)
$ crontab {$ m} {$ h} ++;
}
printf(%02d%-15s * * * \t%s\\\
,$ _,join(',',sort keys%{$ crontab {$ _}} ),$ command)用于排序键%crontab;

用法:

 code> $ ./gencrontab -period = 105 -offset = 1:30 -command = myprogram 
00 05,12,19 * * * myprogram
15 00,03,10,17 * * * myprogram
30 01,08,15,22 * * * myprogram
45 06,13,20 * * * myprogram


I'm not great with Linux, my apologies if this is easy.

I need to run 7 different scripts via cron at 15 minute intervals. I have worked out that each script needs to run every 105 minutes (every 1 hour & 45 minutes) as follows :

Script 1    Script 2    Script 3    Script 4    Script 5    Script 6    Script 7
00:15       00:30       00:45       01:00       01:15       01:30       01:45
02:00       02:15       02:30       02:45       03:00       03:15       03:30

etc...

Now I know I can't just put this:

*/105 * * * * something

Because the minutes will only go up to 59.

Other than writing each one in a line separately, is there an easy way to do this?

Any assistance would be greatly appreciated.

解决方案

at is a good candidate for this problem.

Write a wrapper (or build into your script):

#!/bin/bash
at -f $0 now + 105 minutes 
<program>

Then use at -f <script> <time> for each instance.


Or generate the crontab entries using a script (my first draft was a bit too long to be a oneliner):

#!/usr/bin/perl -Ws
use strict;
use DateTime;
our ($period, $offset, $command);
my ($offh, $offm) = split(/:/, $offset || '0:00');
my %crontab = ();
for (my $i = 0; $i < 1440; $i += $period) {
    my ($h,$m) = split /:/, 'DateTime'->today->set('hour', $offh, 'minute', $offm)->add('minutes', $i)->hms;
    $crontab{$m}{$h}++;
}
printf("%02d %-15s * * *\t%s\n", $_, join(',', sort keys %{$crontab{$_}}), $command) for sort keys %crontab;

Usage:

$ ./gencrontab -period=105 -offset=1:30 -command=myprogram
00 05,12,19        * * *    myprogram
15 00,03,10,17     * * *    myprogram
30 01,08,15,22     * * *    myprogram
45 06,13,20        * * *    myprogram

这篇关于每105分钟运行cron的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆