Cron job run every N hours (where 24 isn't evenly divisible by N) doesn't work as expected - cron

For example, if I have a cron job that I want to run every 9 hours:
0 */9 * * * my_script
The job is executed at 00:00, 9:00, and 18:00; and then the same hours the next day.
What I want is for the job to execute at 00:00, 9:00, 18:00; then 03:00, 12:00, 21:00 the next day -- a true "every 9 hours".
Is there any way make cron job run EVERY 9 hours?

Specifying */9 means that the job runs every 9 hours starting at 00:00. It starts again at 00:00 every day.
There is no syntax in cron to run a job every 9 hours.
What you can do is run a job every 3 hours, and have the command itself examine the current time and only execute 1 time out of 3. Or it can run every hour and execute one time out of every 9. Don't assume that the current time will be exact; it might run a few seconds after the hour.

Related

Run a cron job every 45 minute only between 10 am to 10 pm

I need my crontab to execute every 45 minute of hour between 10 am to 10 pm.
currently I trying this
*/45 10-21 * * *
is this right or not?
This would start the first job at 10:45am last one at 9:45pm and jobs started at 45th min of every hour in between.
You can refer here how cron works https://man7.org/linux/man-pages/man5/crontab.5.html

Cron - Scheduling a workflow to run every hour, except 2 hours between 12 AM and 2 AM

I am using cron and I need to schedule my workflow, so that it runs every day hourly, just except 2 hours from 12 AM until 2 AM. Meaning it needs to run daily from 2 AM and kick off every hour, but just when the clock hits 12 AM it should't run for 2 hours. And then starts back at 2 AM to run every hour.
Would appreciate a solution.
This is a CRON job
0 0 2-23 ? * * *
Description: Job will start at second:00, at the minute:00, every hour between 02 am and 23 pm, of every day
I have found this website useful to create CRON job

Will */5 hours in a Cron job be done at 1pm the next day?

We need a Cron job that runs every 5 hours and that takes a little longer than 4 hours to complete,
If it's scheduled in a Cron job as */5 for the hours part will it run at 1pm the second day?
Ie. Will the first day be 0,5,10,15,20 and then 1,6,11,21, (and so forth...)?
No, it will not.
Assuming the minute part is fixed (which makes the full cron expression into 0 */5 * * *), the cron job will always be triggered at 0th, 5th, 10th, 15th, and 20th hours every day.
In addition to that, the next cron job will be triggered regardless of whether the previous cron job has already been completed or not. For example, if the cron job at 5th hour takes 7 hours to complete, the cron job at 10th hour will still be triggered on time.

How to execute a cron expression for every 2.5 min

I want to create a cron expression which will run the scheduler every 2.5 min of every hour. e.g. 2.5min, 5.0min, 7.5min, 10.0min etc. of every hour.
I am using Spring to create the scheduler. I tried various combination but nothing worked. One of them is as below but it is not working.
#Scheduled(cron = "*/30 */2 * * * *")
Thanks in advance.
That should works for you
0 0/5 0 ? * * *
30 2/5 0 ? * * *
At second :00, every 5 minutes starting at minute :00, at 00am, of every day
At second :30, every 5 minutes starting at minute :02, at 00am, of every day
You are right in this case you need to schedule your task twice using expression like on example.
There is a danger of becoming fixated on the 30 seconds. My problem was that I needed to check 18000 records for updates every month ~ 1 record every 2.5 minutes. I spent too much time trying techniques to run a job at exactly 02:32:30 before I realised that accuracy was not important.
In my situation, I realised I could execute every 2 minutes, updating my full database every 25 days instead of every 31 days.
Alternatively, I could have had 2 cron jobs running every 5 minutes. First, a 2-minute gap, followed by a 3-minute gap.
02:30 02:32 02:35 02:37 02:40 02:42 02:45 02:47
My point is that when the cron job is live, it runs unseen. Obviously, everyone has their own specific problem, but before introducing complexity, consider if it is necessary. As long as the job executes, does it really matter the exact time it ran?

Resuming 0 */3 * * * Crontab process affect hour upon which runs occur?

I have a crontab set up to execute the 0th minute of every 3rd hour every day
The crontab syntax for this is:
0 */3 * * * perl test.pl
Will this always run on the 0th minute of some specific hours of the day that are 3 hours apart. Or, will the time at which I install the new crontab affect the hour that this runs?
This cron will execute on Hours that are divisible by 3 so it should not matter at which time its started. The next occurance of an hour Divisible by 3 will be the first execution of your cron.
As per Chiyaan Suraj's comment, this will run on 0,3,6,9,12,15,18,21 hours.
On your second comment, this will be on hours divisible by 5 (0,5,10,15,20)

Resources