CRON: Run job on particular hours - spring

I have a spring batch application and i am using CRON to set how often this application runs. But the problem i am running into is that i want to run the job on specific hours
3 am
7 am
11 am
3 pm
7 pm
11 pm
As you can see it is every 4 hours but starts at 3 am so i cannot use */4 in the hours section of the timing format as this would start the job at 4am
I have also tried '3,7,11,15,19,23' in the hours section but this does not work either (guessing it only works in the minutes section). Does someone know how i can do this?

Use
#Scedule(cron="0 0 3/4 * * ?")
The Pattern x/y means: where <timepart> mod y = x
or
#Scedule(cron="0 0 3,7,11,15,19,21 * * ?")
According to the Quartz Cron Trigger Tutorial:
The '/' character can be used to specify increments to values. For
example, if you put '0/15' in the Minutes field, it means 'every 15th
minute of the hour, starting at minute zero'. If you used '3/20' in
the Minutes field, it would mean 'every 20th minute of the hour,
starting at minute three' - or in other words it is the same as
specifying '3,23,43' in the Minutes field. Note the subtlety that
"/35" does *not mean "every 35 minutes" - it mean "every 35th minute
of the hour, starting at minute zero" - or in other words the same as
specifying '0,35'.

0 0 3,7,11,15,19,23 * * ?
Fires for 0 minute starting at 3am and ending at 23:00 pm every day.

judging by the two answers above the error i was making was i was keeping the apostrophe at the start and end of my hours... very silly
i managed to solve this by using 3-23/4 for the hour as this starts from 3am and then every other fourth hour (just a different way of doing it to the other answers)

Related

What is "INTERVAL=0" means in Oracle Schedular?

My Oracle DBA have setup a task with following repeat_interval:
Start Date :"30/JAN/20 08:00AM"
Repeat_interval: "FREQ=DAILY; INTERVAL=0; BYMINUTE=15"
Can I ask what is "Interval=0" means?
Does it means this task will run daily from 8AM, and will repeat every 15 mins until success?
I tried to get the answer from Google, but what I find is what is Interval=1, but nothing for 0.
So would be great if anyone can share me some light here.
Thanks in advance!
INTERVAL is the number of increments of the FREQ value between executions. I believe in this case that a value of 0 or 1 would be the same. The schedule as shown would execute once per day (FREQ=DAILY), at approximately 15 minutes past a random hour (BYMINUTE=15, but BYHOUR and BYSECOND are not set).
Schedule has nothing to do with whether or not the previous execution succeeded or not. Start Date is only the date at which the job was enabled, not when it actually starts processing.
If you want it to run every 15 minutes from the moment you enable it, you should set as follows:
FREQ=MINUTELY; INTERVAL=15
If you want it to run exactly on the quarter hour, then this:
FREQ=MINUTELY; BYMINUTE=0,15,30,45; BYSECOND=0
If you want it to run every day at 8am, then this:
FREQ=DAILY; BYHOUR=8; BYMINUTE=0; BYSECOND=0

Cron schedule a task alleatory in a range of hours

I want schedule with cron a task to be ran ONCE in between a certain tange of hours, not every hour in a range.
Example:
I want it may run ONCE between 9 to 12 hours, single time.
Better if I can run the task ONCE between 9 to 12 hours and random minute as well. I don't want can be predictable, but I want keep control about in which range of hours it will be fired.
I tried:
# m h dom mon dow command
00-59 09-12 * * 2 /home/osmc/play.sh
But it ran every minute, between 00 and 59, for every hour between 9 to 12 (not desired)
Any idea how I can do so?
If you like to stick with cron, you could do the following:
0 9 * * 2 sleep $(( RANDOM \% 10800)); /home/osmc/play.sh
The job will start at 9AM, and delays the script for upto 10800 seconds, which is 3 hours.
Another solution found here:
Cron jobs and random times, within given hours

spring cronsequencegenerator pattern for job that run on 9th, 16th, 23th and 30th of every month at 10 O'clock.

I am struggling with the spring cronsequencegenerator pattern for job that run on 9th, 16th, 23th and 30th of every month at 10 O'clock.
Please help.
Spring provides a solution
Go through the documentation of Scheduled annotation and you be able to solve your problem.
Assuming it's 10AM and not 22:00 basic cron exp for you would be
0 10 9,16,23,30 * *
As per documentation cron-like exp is needed
0 0 10 9,16,23,30 * * - with additional first field for seconds.
Depending on specification (implementation) of "cron-like" your needs could suit
0 0 10 9,16,23,30 * ? - with day of the week special symbol
0 0 10 9,16,23,30 * * * - with optional field for year filled in

Java - spring - quatrz cron expression

Java - spring - quatz shedular.
I am trying to write following 2 cron expressions
1- cron expression to execute every day once in early morning by 3AM.
2 -cron expression to execute every after 3 hours
help appriciated
following are expressions i assume will work but have to wait to see.
1- "0 0 0/1 * * ?"
2- "0 3 * * * ?"
1) does not make any sense to me.
2) run in the 3th minute of every hour
Your expressions are wrong. The first one runs every hour while the second runs on every 3rd minute of every hour.
To run every day at 3 AM:
0 0 3 * * ?
I don't understand your second requirement.

bash : Make job every x days

I have a bash script runned every day via cron.
In this bash I want to run some command but only every x day (x is fixed and will be 3, 4 or 5 I don't know at the moment)
I can test if the remainder of the division day of month / x equal 0
day_of_month % x = 0
I will work on a single month but not always between to month, for example months april - may with x = 3
1 2 3 4 5 6 7 8 9 ... 27 28 29 30 31 1 2 3 4 5 6 7 8 9 ... 27 28 29 30
x x x ... x x x x x ... x x
It not a big deal, but is there any other way to do this ?
PS : I don't want to explode my script and make 2 cron jobs (one every day : *, the other evevery x day : */x)
One easy solution would be to use "day of year" instead of day of month. Then there'd be no problem with different month lengths. You'd still get problems at the end of year, of course.
The "day of year" is available via date +%j.
If that is still not acceptable, you can use the seconds since 1970 (date +%s), and divide by days*24*60*60, then use the remainder of that. You'd still get problems with leap seconds, but otherwise it should be correct.
Using cron sounds the like the proper idea actually, because that is what cron is made for.
One idea though to solve your 'across months' problems: don't think in months, instead check whether the time-in-seconds-since-epoch % x*seconds-per-day is 0 ...
I would export a system variable with last date of execution and calculate the interval from it. It wouldn't work after a restart but I assume in this case the interval should be restored.
Have a look at the at command
Using that you can tell a script to call itself at a later date.
From manpage:
For example, to run a job at 4pm three
days from now, you would do at
4pm + 3 days, to run a job at 10:00am on July 31, you would do at
10am
Jul 31 and to run a job at 1am tomorrow, you would do at 1am
tomorrow.
If I had this problem, I would tell cron to run my script once every day, and inside the script I would calculate whether or not work needs to be done. That way, you have full control over it.
For example,
dayno=`date +%d`
let divided=daynum%3
if [ $divided == 0 ] ; then
exit 0
fi
# real work begins here
This script would work on any system that uses bash.
Does your job need to run every day? That is, does it do something on those days when your calculation does not result in a 0. If not, you could set up a job in cron thus:
0 12 3,6,9,12,15,18,21,24,27,30 * * <your job>
and it would run every third day (at 12 noon in my example).
I'm pretty sure that "Feb 30" would be safely ignored.

Resources