How to start cron jobs removing offset? - linux

I am running a cron job which will run at every 5 minutes.
Now Let's say i have started job on 04:02 so it will execute at every 5 minutes so will execute on 04:07, 04:12, 04:17 etc...
Let's say i have started job on 13:18 so it will executed at 13:23, 13:28, 13:33 etc...
But what i want is it should only execute in multiplication of 5 minutes means if i create job on 04:02, it should start executing from 04:05, 04:10 and so on.
And if start job on 13:18, it should start executing from 13:20, 13:25 and so on.
So how to achieve this?

Try this:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * <Your command>

*/5 * * * *
this should be what you want .

Related

Cron job periods

I would like to run the cron job every 20 minutes between 9 AM and 11 PM and once an hour after 11 PM until 9 AM.
cron job settings
I created 2 cron jobs for the same script to achieve my goal. Did I do it correctly?
Thank you.
Not really. There are two issues:
you run the script also on 23:20 and 23:40
you run the script twice from 09:00 till 23:00 every hour.
The way to do it is:
*/20 9-22 * * * command
0 0-8,23 * * * command
You can validate this using crontab guru

Set up cron job every minute CPANEL

I tried to set up the cron job any minute, I tried 3 different commands.
Any of them is not working.
Below is the error:
Run cron job every minute
The syntax is:
* * * * * /path/to/your/script > output

Wait 60 Seconds to Run Cron Job After Reboot Then Run Job Every 10 Minutes

I have a script that I would like to run 60 seconds after initial system reboot and then every 10 minutes after that. I currently need two cron job listings to achieve this:
*/10 * * * * php myscript.php
#reboot /bin/sleep 60; php myscript.php
The first listing will run my cron job immediately after system boot and so I need to have the second listing to account for the on start wait time.
Is there anyway to combine the above two cron listings into one?

How the cron timing is working?

Suppose, current time is 11:42 and i have setup one cron file to run at every 5 minutes.
Then this file will run at which time 11:47 or 11:45?
So basically i am trying to understand that how the cron timing is work?
Edit : it was ran at 11:45, but i don't know the reason behind it
Cron Configuration :
*/5 * * * * wget -O /dev/null http://XXX/index.php?r=controller/action
As you know, cron will run jobs at a specific time.
A cron job will not use the time it was started, only the configuration matters. This means a cron job set to every 5 minutes (like your */5 * * * *) will only ever run at times ending with 0 or 5 (eg: 12:00, 12:05, 12:10), regardless of the time you run it. This makes sense because we want to schedule a job for a specific time.
If you really need a job to run every 5 minutes, with an offset (eg: 11:42, 11:47, 11:52) you will have to give a list in the configuration.
instead of (*/5 * * * *) you would need to use:
(2,7,12,...,57 * * * *), filling ... with all the other numbers.

Cron runs after other cron

I want to set a cron run after an other cron. For example: Cron A finishs at 01:00 PM, cron B will start at 01:01 PM. The problem is I don't know when cron A finishs.
I checked the crontab syntax. It doesn't provide any param for that purpose.
My actual situation is:
# This cron must run first.
? ? * * * /usr/local/bin/php -f /path/select_and_print_to_log_file.php
# two these crons runs at the same time.
0 13 * * * /usr/local/bin/php -f /path/update_user.php
0 13 * * * /usr/local/bin/php -f /path/update_image.php
# This cron runs right after two above cron completes.
? ? * * * /usr/local/bin/php -f /path/select_and_print_to_log_file.php
You can use the batch command inside the first cron to have the second thing being scheduled to run.
Your first job could produce a timestamp when finished.
Then you estimate - for example - that job A needs about 60 to 90 minutes. After 60 minutes, you start job B. Job b looks for the timestamp. If it is present, job B starts, else it waits for a minute and looks again.
After finishing, job B deletes the timestamp, or renames it, maybe from 'todo' to 'done'. You could insert the current date inside the file, to check, whether your estimation is still acceptable, or should be adjusted.
What I do in such cases (commonly a backup scenario where I don't want to thrash the disk by having concurrent backups) is to write a script that cron calls, and in the script have the actual tasks run serially.
Something like:
#!/bin/bash
/usr/local/bin/php -f /path/update_user.php
/usr/local/bin/someOtherTaskToRunSecond
YMMV.

Resources