Cron schedule - Execute job at three different times with no pattern between. Is it possible? - cron

I have a job I want to run using Quartz, which require the time to be specified using a Cron schedule.
The job should run at 17:00, 17:35 and 22:05.
Is it possible to specify these times using a Cron schedule ? I've already looked into the Wikipedia article, but it did not help much. From my point of view, there needs to be a pattern between the times, so that you can specify them using a Cron schedule.
Best regards
Nicolas

You can do it with 3 cron jobs. example :
> crontab -e
00 17 * * * sh /example/script.sh
35 17 * * * sh /example/script.sh
05 22 * * * sh /example/script.sh

Related

Set Cron Job Between Specific Times and Frequency

For example I want to run job every 6 minutes between;
16:34 - 18:45
So it must be running on 16:34, 16:40, 16:46 etc. to 18:40.
When I write
34-45 16-18 * * *
It only works between 16:34-16:45 and 18:34-18:45. But I don't want this one.
Is it possible to make this?
Thank you
Hm, where in your cron line is the "every 6 minutes" part? Also, why would your cron go to 18:40, wouldn't 18:42 be the last time you want it to run? Let me know if I'm not understanding the question correctly.
Anyway though, not sure if it's possible in one cron line, but you could always do something like:
34/6 16 * * *
*/6 17 * * *
0,6,12,18,24,30,36,42 18 * * *
Edit: Or, if you have control and are able to edit the file/executable your cron is running you could do:
*/6 16-18 * * * /path/to/myScript
And then at the very beginning of myScript:
if time < 16:34 or time > 18:45:
exit # kill script

How to start cron jobs removing offset?

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 .

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.

How can make cron job which happen at different hours and minutes

I want to make crontab where script occurs at different minutes for each hour like this
35 1,8,12,15,31 16,18,21 * * 0,1,2,3,4,5,6 python backup.py
I want script to run at 16hour and 31 minutes but it is giving me error bad hour
i want the cron occur at
1:35am , then 16:31, then 21:45
As there is not a pattern that can match the three times, it is not possible to schedule that just with one crontab expression. You will have to use three:
45 21 * * * python backup.py
31 16 * * * python backup.py
35 1 * * * python backup.py
Note also that python backup.py will probably not work. You have to define full path for both files and binaries:
35 1 * * * /usr/bin/python /your/dir/backup.py
Where /usr/bin/python or similar can be obtained with which python.
If the system which you are on has systemd, You can look into systemd timers(https://www.freedesktop.org/software/systemd/man/systemd.time.html). Then you might be able to achieve the randomness using the RandomizedDelaySec setting and an OnCalendar setting which will schedule the service to run every hour or interval you set plus will generate a RandomizedDelaySec at every run so that the interval is random.

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