2

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.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
naruto
  • 327
  • 1
  • 11
  • What has javascript to do with this, which expressions have you tried, did you look into `man cron` or `man crontab` or any other internet site? When you google for `cron monthly` you get super accurate results. – Daniel W. Jan 23 '19 at 17:45
  • 1
    Possible duplicate of [Cron job to run on the last day of the month](https://stackoverflow.com/questions/6139189/cron-job-to-run-on-the-last-day-of-the-month) – Daniel W. Jan 23 '19 at 17:47
  • @DanFromGermany sorry for not exact info but that answer is not what i am looking for. – naruto Jan 23 '19 at 18:19
  • I think you'd have to implement some logic yourself where you check, every month, how many months have passed since a specific date, and if that number modulo 5 is zero, you run your script. – Benjamin W. Jan 23 '19 at 19:06

2 Answers2

1

I couldn't find a way to start on the exact date in the month when it's started. If it is ok it's the first (like example below), you can use this:

Contab guru

“At 00:00 on day-of-month 1 in every 5th month.”

Otherwise play with the values to suit your needs.

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
0

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.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116