Does this cron expression mean every other Sunday? - cron

Does the following cron expression mean "execute every other Sunday?"
0 0 3 ? * 2/1 *
I'm trying to use it with the Spring Quartz scheduler.

The expression you are asking about fires at 3 am Monday to Saturday. From the Quartz Javadoc you could try using the two expressions 0 0 3 ? * 1#1 * and 0 0 3 ? * 1#3 * to execute on the 1st and 3rd Sundays of the month. The D#N syntax lets you pick the Nth day D of the month.

No, I don't think so. I think "2/1" means "Tuesday through Sunday." I'm not sure that it's possible to express "Every other Sunday", because there'd have to be a "week of month" field or something like that.

Related

cron expression - 3 times a day (in one line) cadence

I would like to create a cron expression for 3 times a day - 02:20, 12:30, 22:20
but I want it to be in one line (not 2 lines as I saw that suggested before)
Thanks for your help!
Assuming a pure cron definition (not a cron-based extended library), that is not possible as cron definitions are simple pattern matching and lack conditionals. I.e. you can match "minute=20" and "minute=30" but not "minute=20 if hour=2 or hour=22".
If you're willing to loosen up your requirement a bit you can achieve
e.g. 2:30, 12:30, 22:30 with: 30 2,12,22 * * *
An other option is over-express and then implement the condition in the triggered code.
20,30 2,12,22 * * * would give you 2:20, 2:30, 12:20, 12:30, 22:20, 22:30, then check in the triggered code whether the trigger corresponds to one of 02:20, 12:30, 22:20.

How to create cron job that is executing every 3 months?

I am using Hangfire in ASP.NET Core for Cron (recurring) Jobs, and I need to create a job that runs every three months starting from a given start date.
So if the start date was 15-Nov-2019, it should run on 15-Nov-2019, 15-Feb-2020, 15-May-2020 and so on and so forth.
And I need it to run every 3 months forever.
So I tried the following cron expression for this: "0 0 15 11/3 ?" or "0 0 15 11/3 *"
But after testing it on this translating site, it tells me that it will run on the following dates:
2019-11-15
2020-11-15
2021-11-15
2022-11-15
2023-11-15
So, if that is true, then how to make it run every three months starting from 15-Nov-2019 as described above and keep running forever?
The month field in cron takes a number between 1 and 12; depending on the cron implementation used, you could use an explicit list for the month field:
0 0 15 2,5,8,11 *
or a range with a step:
0 0 15 2-12/3 *
crontab.guru seems to support a single value with a step as well, but the crontab man page doesn't mention this style, so it might or might not work:
0 0 15 2/3 *
If you want to be able to set this up more than three months before you want it to run for the first time, you have to manually check the date; in shell (using GNU date), you would do something like this:
0 0 15 2-12/3 * [ $(date +%%s) -gt $(date -d '2019-11-01' +%%s) ] && yourcommand
This compares the current date to November 1st, 2019; if it is greater than that, the command is run.
Simple solution is to use the following command:
0 0 15 */3 *
It is very straight forward.Here's the output for your satisfaction from crontab.guru website
output of cron job

run cron job between 00:00 - 00:02 - 04:00 - 23:59 on every hour

I want to run a cron job between 0:00am - 02:00am - 04:00am and 23:59am on every hour.
I want to know if this is the correct syntax.
0,0-59 0-2,4-23/1 * * *
Thanks!
No, your syntax is not correctly formatted.
You can use:
0 0 0/1,0-2 ? * *
This will run according to the following rules:
At second :00, at minute :00, every hour between 00am and 02am,
and every hour starting at 00am, of every day
You can check CRON syntax with an explanation at:Cron Expression Generator & Explainer.
Also, I think this site has a really good breakdown to help understand what each section of the CRON expression relates to.
Edit: I just noticed you had the second part about running at 23:59. For this you will need to set up a second CRON job:
0 59 23 * * ? *
Use Case: At 23:59:00pm every day

cron expression for every hour starting from specific time

I am able to schedule using this cron expression using nodejs cron-job every one hour (starting from "now").
But I need to set q cron every one hour starting from a specific time. E.g let's say starts from 3:30 AM. can this be done?
The / character allows you to give two expressions to a cron part. The first is a "starting at" argument and the second is "every X units". So, a cron that will run every hour, starting at 03:30 (I.e., at 03:30, 04:30, 05:30, etc.) would look like this:
0 30 3/1 * * * *
You can try this:
30 3/1 * * * * *
Just to add to Mureinik's answer, just "starting at" on the first argument is non-standard and it may not work with every cron. The standard format should be
startingAt-endingAt/forEvery
example: 30 3-23/1 * * *

Cron expression for one day occurs every hour

I need cron expression which allows to me run scheduler by the following rules:
Starts 12:00 am on Friday (pacific time)
ends 12:00 am on Saturday (pacific time)
And between these two dates it must occurs every hour
i can write something like "0 0 12/1 ? * FRI-SAT" but ofcourse it is not correct.
How to set simple range from 12-00 FRI to 12-00 SAT?
Try this expression
0 0 12-23,00-12 ? * FRI,SAT
and you can verify the next scheduled time here at http://www.cronmaker.com/ by entering this expression.
I'm guessing that you will probably need to set up two triggers, one that starts at 12:00 pm Friday and ends at midnight and triggers every hour and one for Saturday starting at 00:00 am and ends at 12:00 pm. So something like this
0 0 12-23 ? * FRI
0 0 0-12 ? * SAT
Cron Trigger Tutorial
Edit
Also have a look at Cron Maker as it will generate the cron expression for you. and also show when it will trigger.
If a cron expression is not easily discernible you can always build your own trigger.
I'm thinking something like this may help:
var jobDetail;
var days = new DayOfWeek[] {DayOfWeek.Friday, DayOfWeek.Saturday};
var trigger = TriggerBuilder.Create()
.ForJob(jobDetail)
.WithDailyTimeIntervalSchedule(x => x.WithIntervalInHours(1)
.OnDaysOfTheWeek(days)
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 0)));

Resources