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

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

问题描述

我需要创建一个将在每个月的最后一天运行的 CRON 作业.我将使用 cPanel 创建它.

I need to create a CRON job that will run on the last day of every month. I will create it using cPanel.

感谢任何帮助.谢谢

推荐答案

可能最简单的方法是简单地做三个独立的工作:

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

并修改脚本以处理上个月的数据.

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

这消除了您在确定哪一天是该月的最后一天时可能遇到的任何麻烦,并确保该月的所有数据都可用(假设您正在处理数据).在每个月的最后一天到午夜 5 分钟跑步,您可能会发现您错过了从那时到午夜之间发生的任何事情.

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

应该是一个好的开始,假设您有一个相对智能的date程序.

should be a good start, assuming you have a relatively intelligent date program.

如果您的 date 程序不够先进,无法为您提供相对日期,您可以组合一个非常简单的程序来为您提供每月的明天(您不需要datefull幂),例如:

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
", 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

尽管您可能需要考虑添加错误检查,因为如果出现问题,time()mktime() 都可以返回 -1.为简单起见,上面的代码没有考虑到这一点.

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天全站免登陆