Set Cron Job Between Specific Times and Frequency - cron

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

Related

Crontab new line just don't work as it should?

I have set crontab on every 24 hrs should run a single php command, instead it runs the command like 20+ times a day. It doesn't work as it should. Is it really wrong?
* */12 * * * php /var/www/mything.php
I added this on new crontab line? Is the line correct?
0 */12 * * * php /var/www/mything.php
should run the command every 12 hours (0 and 12 o'clock)
0 12 * * * php /var/www/mything.php
should run the command at 12 o'clock every day

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.

Running a cron job 3 times (1 pm, 2 pm and 3 pm for example)?

I am not sure how to run a cron job at 3 specific hours every day. I want to run it at 1pm, 2 pm and 3pm.
Is it possible, using a single expression?
you may use this:
# m h dom mon dow command
0 13,14,15 * * * /home/user/command
your /home/user/command will be run at 13:00, 14:00 and 15:00
As lenik stated, it can be done in single expression.
0 13,14,15 * * * <your-script-to-run>
Check this geedkstuff link for more examples
While the given answers are correct, an unexperienced user might not know where to put this expression. You have to edit the crontab file, like:
crontab -e
There you add
0 13,14,15 * * * /home/user/command
to execute your command at 13:00, 14:00 and 15:00. Also note that user has to be substituted with the user account the command is executed in.
You can try the following as well:
0 13-15 * * * /home/apps/sample.sh
To anyone landing here --> useful tool:
https://crontab.guru/
Please prefer range+step over commas:
Example: Run every 2h from 9h to 16h
m h dom mon dow command
0 9-16/2 * * * /home/user/command
Also applicable to minutes:
m h dom mon dow command
10-30/10 9-16/2 * * * /home/user/command
Crontab guru shows what it means, and the next scheduled jobs.
For example I typed this cron at 10h05:

Cron job question

I want to run a php script every 10 minutes, between the hours of 9:30AM - 4:00PM
I googled before asking, and didn't have any success.
Anyone know how to do this? Or point me in the right direction?
Thank you
Try the following three lines in crontab
0,10,20,30,40,50 10-15 * * * # Every 10 minutes for the hours 10am - 3pm
0 16 * * * # 4pm
30,40,50 9 * * * # and 9:30, 9:40, 9:50
Run it from cron in every 10 minutes, check th date in PHP do nothing if it's outside the range.
*/10 * * * * /usr/bin/php /path/to/your/script.php

Restrict Crontab job between a particular interval

I have a ruby file and I need to run it once in every 3 hours.
This code works fine:
27 */3 * * * /path/to/ruby -rubygems /path/to/ruby_file.rb
And I don't want this file to run between midnight 12.00 to morning 8.00
How can I set it?
27 9-23/3 * * * should work on most modern variants of Unix.
27 9-23/3 * * * /path/to/ruby -rubygems /path/to/ruby_file.rb
should do the trick.
A good summary of it all can be found in this wikipedia article.
*/15 10-20 * * * /usr/bin/php /tmp/myscript.php
*/6 6-9,21-23,0-2 * * * /usr/bin/php /tmp/myscript.php
More complex cron for you my friend.
Notice: cron do not jump over 23-0
2-10/4 like 2,8 or 2,6,10 ?
I think the seconds

Resources