cron作业在每月的最后一天运行 [英] Cron job to run on the last day of the month

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

问题描述

我要创建将在每月的每一个最后一天运行cron作业。
我会的cPanel创建它。

I need to create a cron job that will run on the every last day of the month. I will create it from cpanel.

任何帮助是AP preciated。
谢谢

Any help is appreciated. Thanks

推荐答案

也许最简单的方法就是简单地做三个独立的工作:

Possibly the easiest way is to simply do three separate jobs:

55 23 30 4,6,9,11        * myjob.sh
55 23 31 1,3,5,7,8,10,12 * myjob.sh
55 23 28 2               * myjob.sh

这将在2月28日,虽然即使在闰年运行,​​因此,如果这是一个问题,你需要找到另一种方式。

That will run on the 28th of February though, even on leap years so, if that's a problem, you'll need to find another way.

然而,它通常是既大大简化和正确尽快运行作业上的第一个的各月的一天,是这样的:

However, it's usually both substantially easier and correct to run the job as soon as possible on the first day of each month, with something like:

0 0 1 * * myjob.sh

和修改脚本来处理的 previous 的一个月的数据。

and modify the script to process the previous month's data.

这将删除你可能会找出哪一天是最后一个月遇到任何麻烦,也确保了当月所有可用的数据,假设你正在处理数据。五分钟运行到午夜在每月的最后一天可能会看到你缺少什么,然后到午夜之间发生了。

This removes any hassles you may encounter with figuring out which day is the last of the month, and also ensures that all data for that month is available, assuming you're processing data. Running at five minutes to midnight on the last day of the month may see you missing anything that happens between then and midnight.

这是通常的方式做到这一点无论如何,对于大多数结束一个月的工作。

This is the usual way to do it anyway, for most end-of-month jobs.

如果您仍然确实的要在每月的最后一天运行,一种选择是简单地检测如果明天是第一个(无论是作为脚本的一部分,或者在crontab本身)。

If you still really want to run it on the last day of the month, one option is to simply detect if tomorrow is the first (either as part of your script, or in the crontab itself).

那么,是这样的:

55 23 28-31 * * [[ "$(date --date=tomorrow +\%d)" == "01" ]] && myjob.sh

应该是一个良好的开端,假设你有一个比较聪明的日期程序。

如果你的日期程序不是很够先进,给你相对日期,你可以把一个很简单的程序来给你一个月的明天的日子(你不需要的完全日期),如功率:

If your date program isn't quite advanced enough to give you relative dates, you can just put together a very simple program to give you tomorrow's day of the month (you don't need the full power of date), such as:

#include <stdio.h>
#include <time.h>

int main (void) {
    // Get today, somewhere around midday (no DST issues).

    time_t noonish = time (0);
    struct tm *localtm = localtime (&noonish);
    localtm->tm_hour = 12;

    // Add one day (86,400 seconds).

    noonish = mktime (localtm) + 86400;
    localtm = localtime (&noonish);

    // Output just day of month.

    printf ("%d\n", localtm->tm_mday);

    return 0;
}

和再使用(假设你已经把它叫做 tomdom 为明天的天月):

and then use (assuming you've called it tomdom for "tomorrow's day of month"):

55 23 28-31 * * [[ "$(tomdom)" == "1" ]] && myjob.sh

虽然你可能要考虑增加错误,因为这两个时检查() mktime()根据可以返回 1 。的code以上,为简单起见,没有考虑到这一点。

Though you may want to consider adding error checking since both time() and mktime() can return -1 if something goes wrong. The code above, for reasons of simplicity, does not take that into account.

这篇关于cron作业在每月的最后一天运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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