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

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)

Related

how to run a cron job at a certain frequency starting from a specific date?

I tried using both npm packages "cron" and "node-schedule" to make this work, but i couldn't.
Let's say i want to start the cron job at 6.30PM, and starting from 6PM i want it to be executed every 2 hours.
First is that i couldn't make it work if it is not an exact hour , meaning if it is not (1PM,2PM,8PM.....)
So i supposed that it will run at 6PM sharp and not 6.30PM
I tried this pattern:
"0 18-23/2 * * *" which is supposed to work from 18 to 23, escaping two hours [18,20,22,00] but once it's past midnight it will stop til it's 18h again. i want it to keep executing every 2 hours (meaning it executes at 2AM and so on ...)
IS that possible with cron jobs ?
You could do a basic shell script to check if the current date and time are greater than or equal to the date and time you specify before executing the command.
Please note that the date and time are specified in UTC. This will cause the cron to run every 2 hours 30 minutes past the top of the hour but it wont actually activate until after 6PM December 3rd 2019 UTC. Also note that the date command may vary with different flavors of nix* / BSD / OSX.
30 */2 * * * if [ $(date --utc +%s) -ge $(date --utc --date "2019-12-03T18:00:00" +%s) ]; then (command) fi
I believe you are talking about using crontab to schedule jobs.
crontab is designed to let you set the frequency based on many things but it is not designed to become "enabled" a certain time.
so the following will run every two hours
0 0,2,4,6,8,10,12,14,16,18,20,22 * * * (command)
Lets say it is 1st of the month and you want it to run every two hours starting at 6 pm
I would do the following
0 18,20,22 1 * * (command)
0 0,2,4,6,8,10,12,14,16,18,20,22 2 * * (command)
This means that today (based on day of month it would run starting at 6 and tomorrow it will run every two hours. Then sometime tomorrow I would edit the file and remove the first line and change the '2' for the day of month to a '*' (to match the first line I showed you) -- now it will always run every two hours.
tldr; edit crontab to work for two days and then edit it after the go live to work for every day.

Run CRON job everyday at specific time

Right now i am running my cron job everyday at 3.00PM
0 15 * * *
But I want to run my cron job twice in a day. 10.30AM and 2.30PM
0 30 10 * * *
I believe this command will run at 10.30AM. How should i run it in 2.30PM?
Cron utility is an effective way to schedule a routine background job at a specific time and/or day on an on-going basis.
Linux Crontab Format
MIN HOUR DOM MON DOW CMD
Example::Scheduling a Job For a Specific Time
The basic usage of cron is to execute a job in a specific time as shown below. This will execute the Full backup shell script (full-backup) on 10th June 08:30 AM.
Please note that the time field uses 24 hours format. So, for 8 AM use
8, and for 8 PM use 20.
30 08 10 06 * /home/yourname/full-backup
30 – 30th Minute
08 – 08 AM
10 – 10th Day
06 – 6th Month (June)
*– Every day of the week
In your case, for 2.30PM,
30 14 * * * YOURCMD
30 – 30th Minute
14 – 2PM
*– Every day
*– Every month
*– Every day of the week
To know more about cron, visit this website.
From cron manual http://man7.org/linux/man-pages/man5/crontab.5.html:
Lists are allowed. A list is a set of numbers (or ranges) separated
by commas. Examples: "1,2,5,9", "0-4,8-12".
So in this case it would be:
30 10,14 * * *
you can write multiple lines in case of different minutes, for example you want to run at 10:01 AM and 2:30 PM
1 10 * * * php -f /var/www/package/index.php controller function
30 14 * * * php -f /var/www/package/index.php controller function
but the following is the best solution for running cron multiple times in a day as minutes are same, you can mention hours like 10,30 .
30 10,14 * * * php -f /var/www/package/index.php controller function

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

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)

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

Crontab syntax for non-aligned hourly range?

If I want to schedule a job to occur every five minutes between 9 until 11 pm I can use the following cron trigger:
0/5 21-22 * * *
(or something like 5,10,15,20,25,30,35,40,45,50,55 21-22 * * * for finer control over the minutes if needed).
Is there a way to specify "every five minutes from 9:30 until 11:30"? The trickiness revolves around having e.g. 5 in the minutes field yet skipping it if the hour is 21, and I'm not immediately aware of any way to achieve that.
A simple, workaround would be to add more scheduled jobs in cron...
i.e.
30,35,40,45,50,55 21 * * * /job_to_run
*/5 22 /job_to_run
5,10,15,20,25,30 23 * * * /job_to_run
p.s. Cron usually has the following order:
Minutes Hour DayOfMonth Month DayOfWeek Command

Resources