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

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)

Related

Schedule cron job from 9:21AM till 02:30PM every 2 minutes

I want to schedule a cron job to run from 9:21 AM till 02:30 PM every 2 minutes from Monday to Friday. How can I do this ?. I know the following can do this from 9 AM till 4 PM, how can I modify this to achieve the above condition.
Thanks in advance.
*/2 09-16 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
This is actually the start of the right cron expression. To get what you're looking for, you'll actually have to combine several scheduled expressions, I believe
I highly recommend using the tool Crontab Guru to check your crontab expressions!
# “At every 2nd minute from 21 through 60 past hour 9 on every day-of-week from Monday through Friday.”
21-60/2 9 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
# At every 2nd minute past every hour from 10 AM through 1:59 PM on every day-of-week from Monday through Friday.
*/2 10-13 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
# At every 2nd minute from 0 through 30 past hour 14 on every day-of-week from Monday through Friday.
0-30/2 14 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
Another note that could be helpful is checking the cron mail (I see you're logging, which is great!) but the system usually delivers cron mail detailing cron jobs as well.
Typically this is found in /var/spool/mail/{username}
There is no indication in the man page (man 5 crontab) that what you require is supported in any single line specification, as any ranges that you set will be applied for each time field (e.g. minute, hour) separately, so for example 21-30/2 9-14 ... would mean to run at 21,23,25,27,29 minutes past each of those hours.
You can of course achieve the desired effect using multiple lines:
21-59/2 9 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
1-59/2 10-13 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
1-30/2 14 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
In this case, it is helped a little by the fact that the interval is a factor of an hour, so at least the middle of these three lines will ensure regular intervals during the period 10:01 - 13:59. If you had an interval of, say, 7 minutes, then you would need even more lines to ensure a completely regular interval throughout.
Note also the following comment in the manual page:
The crontab syntax does not make it possible to define all possible
periods one could image off. For example, it is not straightforward to
define the last weekday of a month. If a task needs to be run in a
specific period of time that cannot be defined in the crontab syntaxs
the best approach would be to have the program itself check the date
and time information and continue execution only if the period matches
the desired one.
So you could adopt this approach and just use for example:
1-59/2 9-14 * * 1-5 /temp/test_cron.sh >> /temp/test_cron.log
and perform some test in your shell script (or perhaps a wrapper script) such as:
hhmm=`date +%H%M`
if [ $hhmm -lt 0930 -o $hhmm -gt 1430 ]; then exit; fi
(here we are treating hhmm as a 4-digit decimal number)

How do I write a cron which run only on any hour and half (xx:30)

How to write a cron which run only on any hour and half (xx:30) ?
For example it would run only at 00:30, 01:30, 02:30, on so on until 23:30.
The first field in a crontab entry is the minutes. You want your job to run whenever the "minutes" part of the current time equals 30, so you need a 30 in the first field. You want it to run at such a time regardless of what the hours, days, or months are, so you put * in the remaining fields.
30 * * * * /my/job

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.

Is there any way to specify periods with cron without setting up multiple instances?

I want a cron job to run between 7 AM and 10 PM, every day, at 5 minute intervals. Normally this would be okay if it was 5 minute intervals, because you could just use "/5", but is there a way to specify it as 5 minute intervals but skip the times between 10PM and 7AM?
The minutes are specified separately from the hours in crontab. Specify minutes using the periodic notation, and the hours as a range.
*/5 7-21 * * * /path/to/script
Below would help for Quartz scheduler
0 00/5 7-21 * * ?

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

How do you run a cron job every minute only between office hours(10 am to 5pm)
I checked this thread Run a cron job every minute only on specific hours? but it doesn't answer my questions.
This should be correct:
* 10-16 * * 1-5 /path/to/my-script
So every single minute, between and including 10am and 5pm, every day in every month that is a day between and including monday to friday. Obviously "office hours" is a fuzzy expression, many people have other schedules ;-)
Unfortunately I fail to see an easy solution to get the script executed also exactly on 5pm...
* 10-16 * * * /path/to/executable/file argument_1 argument_2
Yes, you can define hours range.
Someone tried to edit my answer but as documentation says hours in range are inclusive http://team.macnn.com/drafts/crontab_defs.html so don't change 16 to 17.
It does,
Access your shell script and add the following
* 10-17 * * *
This means run every min, between these hours, on every day, of every month etc

Resources