Cron job time syntax - cron

I have a cron job that processes an action for several records in my database. I want it to process each record with a 5 minute delay, then repeat the process every 12 hours. What is the syntax that I need to use to make this happen? For example, if I have 5 rows in my database that the cron job will process. I want it to process the first row, then process the next row 5 minutes later, then process the next row 5 minutes later, etc. until all rows have been processed. Then repeat the whole process every 12 hours. I tried using */5 */12 * * * but it did not work.

It won't work the way you have configured.
if I have 5 rows in my database that the cron job will process. I want it to process the first row, then process the next row 5 minutes later, then process the next row 5 minutes later, etc
Write a shell scrip to achieve above goal. Cron won't do it for you. Hint: use sleep function in your scrip to wait for 5 minutes before processing next record.
Then repeat the whole process every 12 hours
use * */12 * * * in cron to let your shell script run after every 12 hrs.
So, in short, Cron will trigger a run of you script very 12 hrs AND your script has the logic to wait for 5 minutes between processing any two consecutive DB records.

Related

Cron Expression: starting a job after completion of another job

I have to perform 2 jobs - A and B. Job 'A' is to be prformed at 9:00 am of every weekday. I dont know the duration for job 'A' though, duration may vary.
Also I want to perform job 'B' after 3mins of completion of job 'A'.
Can anyone suggest the cron expression for this please.
Assuming you are trying to run the second job three minutes after the first job completes, let's say you have Job A which involves calling /home/user/job_a.sh and then once that completes, you want to run /home/user/job_b.sh. Instead of trying to set up two different Cron jobs, you could just make a separate script, say job_c.sh. And all job_c.sh does is run Job A, wait three minutes and then run Job B.
Basically, rather than calling two Cron Jobs and trying to sort out timing for both of them, you can just establish one Cron Job which runs both jobs.
On the other hand, if you want to run the second job three minutes after the first one starts then you might as well create two Cron Jobs with three minutes between them which would look something like this:
00 9 * * 1-5 /home/user/job_a.sh
03 9 * * 1-5 /home/user/job_b.sh

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.

Spring Scheduled job with cron, only for the next 3 days

I am trying to run a method every 10 minutes for the next 3 days and only that.
I have tried this :
cron.expression=0 */10 * * * ?
This will run every 10 minutes every day, every month.
But I only want it to be limited to the next 3 days starting from NOW.
I just can't find how to use (now -> 3 days) in cron
I am using this website and Spring scheduler Spring scheduler doc without success
Based on the Spring specs you can't define this logic directly in to the cron record. You should add such logic in to the program you run with this cron

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?

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

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.

Resources