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

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:

Related

Created cron job to run every 2 mint

I have configured cron job but it's not working.
I wanted to run the myfile.sh script for every 2 mint and below are my configuration in crontab.
# m h dom mon dow comman
2 * * * * /home/ubuntu/myfile.sh
myfile.sh is executable and contains below lines of code
#!/bin/bash
mysqldump -u[user] -p[password] --single-transaction --routines --triggers --all-databases > /home/ubuntu/backup_db10.sql
Is there anywhere we need to add configure anything?
You're running the script at two minutes past every hour. As in 1:02, 2:02 and so on.
You can change it to something like
*/2 * * * * /home/ubuntu/myfile.sh
to run it every two minutes.
A bit more info can be found here.

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

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

Running a cron job at 2:30 AM everyday

How to configure a cron job to run every night at 2:30? I know how to make it run at 2, but not 2:30.
crontab -e
add:
30 2 * * * /your/command
To edit:
crontab -e
Add this command line:
30 2 * * * /your/command
Crontab Format:
MIN HOUR DOM MON DOW CMD
Format Meanings and Allowed Value:
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6
CMD Command Any command to be executed.
Restart cron with latest data:
service crond restart
As seen in the other answers, the syntax to use is:
30 2 * * * /your/command
# ^ ^
# | hour
# minute
Following the crontab standard format:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
It is also useful to use crontab.guru to check crontab expressions.
The expressions are added into crontab using crontab -e. Once you are done, save and exit (if you are using vi, typing :x does it). The good think of using this tool is that if you write an invalid command you are likely to get a message prompt on the form:
$ crontab -e
crontab: installing new crontab
"/tmp/crontab.tNt1NL/crontab":7: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit? (y/n)
If you have further problems with crontab not running you can check Debugging crontab or Why is crontab not executing my PHP script?.
An easy way to write cron is to use the online cron generator
It will generate the line for you. One thing to note is that if you wish to run it each day (not just weekdays) you need to highlight all the days.
As an addition to the all above mentioned great answers, check the https://crontab.guru/ - a useful online resource for checking your crontab syntax.
What you get is human readable representation of what you have specified.
See the examples below:
30 2 * * * (answer of this question)
#daily
59 23 31 12 *
30 2 * * * wget https://www.yoursite.com/your_function_name
The first part is for setting cron job and the next part to call your function.
30 2 * * * Every Day at 2:30 Am
30-31 2 * * * Every Day at 2:30 -31 am
Along with he answers its important to understand the cron expressions , i face a lot of difficulty in understanding .
But an intuitive way to understand is given here .

setup cron tab to specific time of during weekdays

I am trying to setup a cron job on a Ubuntu server. We want the cron job to run the script at certain times of the day and on some specific days of the week. For example, we want to setup a cron job that runs the script with the following sequence:
Execute the script every 2 minutes from 9 am to 2 pm during the weekdays.
This is what I have been able to do so far:
*/2 09-14 * * * /path_to_script
What should I do for the weekdays?
Same as you did for hours:
*/2 09-18 * * 1-5 /path_to_script
0 and 7 stand for Sunday
6 stands for Saturday
so, 1-5 means from Monday to Friday
You state 2pm in your requirement, hour range should end at 14 instead of 18 (which is 6pm).
*/2 9-14 * * 1-5 /path_to_script
man crontab
http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5
In fact the last hour you want the script to run is 13:00 to 13:59, so you want:
*/2 9-13 * * 1-5 /path_to_script
meaning the first runtime will be 9:00, then 9:02, and so on until 13:58 which will be the last run as 14:00 is not included.

Resources