Search code examples
cronagenda

Is there a Cron expression to schedule task after every 5 months across year boundaries?


I have an agenda job that I want to schedule for every five months. Suppose I started that job on Jan 20th, so now the schedule should be Jan 20th 2019, June 20th 2019, Nov 20th 2019, April 20th 2020 and so on.

Agenda uses cron for scheduling.

The problem with 00 00 20 1,6,11 * is that it will never run in April, this will run
at 2019-06-01 00:00:00
then at 2019-11-01 00:00:00
then at 2020-01-01 00:00:00
then at 2020-06-01 00:00:00
then at 2020-11-01 00:00:00.

Another expression that I used is 00 00 20 */5 *. The next run times are
at 2019-06-20 00:00:00
then at 2019-11-20 00:00:00
then at 2020-01-20 00:00:00
then at 2020-06-20 00:00:00
then at 2020-11-20 00:00:00

but they are not the month that I want it to run, i.e., at a regular interval of five months.


Solution

  • Cron can't do this directly, but you can move that logic into a script that you call every month. Your cron job could look like

    0 0 20 * * monthcheck 2019 1 5
    

    and monthcheck is a script somewhere in your path with this content:

    #!/usr/bin/env bash
    
    baseyear=$1
    basemonth=$2
    interval=$3
    
    read -r year month <<< "$(date '+%Y %-m')"
    
    if (( (12*(year-baseyear) + (month-basemonth)) % interval == 0 )); then
        echo "Run the script"
    fi
    

    This takes a base date specified by year and month (baseyear and basemonth, supplied as arguments with 2019 and 1 in the crontab entry) plus an interval in months (interval, value 5 in the example).

    It then reads the current year and month into year and month, checks how many months have passed since the basedate, and if that difference is a multiple of interval (modulo division by the interval is 0), then it runs your script (just an echo command in this example).

    The formatting string for date uses %-m to avoid zero padding for the month (gets 1 instead of 01). This feature might not be present in every date – I'm using GNU date here.