How to run a cron job on every Monday, Wednesday and Friday? - cron

How can one run a cron job for every Monday, Wednesday and Friday at 7:00 pm?

Here's my example crontab I always use as a template:
# Use the hash sign to prefix a comment
# +---------------- minute (0 - 59)
# | +------------- hour (0 - 23)
# | | +---------- day of month (1 - 31)
# | | | +------- month (1 - 12)
# | | | | +---- day of week (0 - 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
#--------------------------------------------------------------------------
To run my cron job every Monday, Wednesday and Friday at 7:00PM, the result will be:
0 19 * * 1,3,5 nohup /home/lathonez/script.sh > /tmp/script.log 2>&1
source

Use crontab to add job
crontab -e
And job should be in this format:
00 19 * * 1,3,5 /home/user/somejob.sh

The rule would be:
0 19 * * 1,3,5
I suggest that you use http://corntab.com for having a very convenient GUI to create your rules in the future :)

This is how I configure it on my server:
0 19 * * 1,3,5 root bash /home/divo/data/support_files/support_files_inc_backup.sh
The above command will run my script at 19:00 on Monday, Wednesday, and Friday.
NB: For cron entries for day of the week (dow)
0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday

Use this command to add job
crontab -e
In this format:
0 19 * * 1,3,5 /path to your file/file.php

Use crontab to add job
0 0 9 ? * MON,WED,FRI *
The above expression will run the job at 9 am on every mon, wed and friday.
You can validate this in : http://www.cronmaker.com/

Have you tried the following expression ..?
0 19 * * 1,3,5

Related

Crontab to run at two hours out of the day, but for one of those hours exclude certain days of the week

I need to change the below schedule to exclude Tuesday and Thursday for only the 2nd hour.
0 2,20 * * *
I want to run a job everyday at 2 am and 8pm, but on Tuesdays and Thursdays exclude the 2 am job.
The easiest answer is going to be two separate entries in your crontab:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* 2,20 * * 0,1,3,5,6 root /path/to/command.sh
* 20 * * 2,4 root /path/to/command.sh
Make sure that command.sh isn't in an /etc/cron.* folder.
I think all you need is
0 2,20 * * 1,3,4,6,7
the numbers at the end are the days of the week. O being Sunday.

How can I run sub-minute cron jobs?

I would like to run a cron job every Wednesday at 10:31:10, but I just learned that crontab cannot run sub-minute jobs, so the closest I can get is 10:31 a.m. with the below code:
31 10 * * WED /file/to/run.py
Is it possible to hack around this, or are there other alternatives to cron that could do the job?
The easiest solution is to sleep for 10 seconds:
# .----------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
31 10 * * 3 sleep 10 && /file/to/run.py
You can't. Cron has a 60 sec granularity.
You can though build a SH script that sleeps for ten seconds and then does X, set your cron job to run a script at 10:31 AM on a wednesday that then sleeps for 10 seconds, then do x.

Crontab job executed every day instead every month

Three days ago I installed the following crontab job with crontab -e:
# execute weekly
0 2 2-31 * 7 sh /home/user/folder/myscript.sh week > /home/user/.crontablog/crontab.log
It's supposed to be executed every sunday night at 2am except the 1st of the month. However it's executed every night at 2am. What's my mistake? I tried 0 instead of 7 for Sunday with the same result :/
Thank you.
Since the format of crontab is like this:
+---------------- 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
To execute it every week on Sunday irrespective of the month you need to write it like this:
0 2 * * 7 sh /home/user/folder/myscript.sh week > /home/user/.crontablog/crontab.log
first you can analyze the content of /var/log/cron, grepping for your script to see what is going on.
I suggest you use the following syntax
0 2 * * sun /home/user/folder/myscript.sh week
having given the +x permission on the script file.
Cheers
Now that you respecified your question in the comments you need to do TWO things:
as I said above, use 0 2 * * 7 to run at 2:00am every Sunday; 7 and 0 are equivalent
inside your shell script use an additional test to quietly exit on the first of the month.
That test could be as simple as
test $(date +%d) == "01" && exit
but you could of course also make it a proper if ... with more echo and verbosity.
If you want to run the script every sunday night at 2am, but only if it's not the first day in the month, then you have to use this syntax:
0 2 * * 0 /usr/bin/test $(/bin/date +\%e) -ne 1 && your_command
The test utilliy finally check if it's the first day in month and your_command is only exected if it's not.

How to run crontab job every week on Sunday

I'm trying to figure out how to run a crontab job every week on Sunday. I think the following should work, but I'm not sure if I understand correctly. Is the following correct?
5 8 * * 6
Here is an explanation of the crontab format.
# 1. Entry: Minute when the process will be started [0-60]
# 2. Entry: Hour when the process will be started [0-23]
# 3. Entry: Day of the month when the process will be started [1-28/29/30/31]
# 4. Entry: Month of the year when the process will be started [1-12]
# 5. Entry: Weekday when the process will be started [0-6] [0 is Sunday]
#
# all x min = */x
So according to this your 5 8 * * 0 would run 8:05 every Sunday.
To have a cron executed on Sunday you can use either of these:
5 8 * * 0
5 8 * * 7
5 8 * * Sun
Where 5 8 stands for the time of the day when this will happen: 8:05.
In general, if you want to execute something on Sunday, just make sure the 5th column contains either of 0, 7 or Sun. You had 6, so it was running on Saturday.
The format for cronjobs is:
+---------------- 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
You can always use crontab.guru as a editor to check your cron expressions.
Following is the format of the crontab file.
{minute} {hour} {day-of-month} {month} {day-of-week} {user} {path-to-shell-script}
So, to run each sunday at midnight (Sunday is 0 usually, 7 in some rare cases) :
0 0 * * 0 root /path_to_command
The crontab website gives the real time results display: https://crontab.guru/#5_8_*_*_0
When specifying your cron values you'll need to make sure that your values fall within the ranges. For instance, some cron's use a 0-7 range for the day of week where both 0 and 7 represent Sunday. We do not(check below).
Seconds: 0-59
Minutes: 0-59
Hours: 0-23
Day of Month: 1-31
Months: 0-11
Day of Week: 0-6
reference: https://github.com/ncb000gt/node-cron
I think you would like this interactive website, which often helps me build complex Crontab directives: https://crontab.guru/
Cron job expression in a human-readable way crontab builder
#weekly work better for me!
example,add the fellowing crontab -e ,it will work in every sunday 0:00 AM
#weekly /root/fd/databasebackup/week.sh >> ~/test.txt
10 * * * Sun
Position 1 for minutes, allowed values are 1-60
position 2 for hours, allowed values are 1-24
position 3 for day of month ,allowed values are 1-31
position 4 for month ,allowed values are 1-12
position 5 for day of week ,allowed values are 1-7 or and the day starts at Monday.
* * * * 0
you can use above cron job to run on every week on sunday, but in addition on what time you want to run this job for that you can follow below concept :
* * * * * Command_to_execute
- � � � -
| | | | |
| | | | +�� Day of week (0�6) (Sunday=0) or Sun, Mon, Tue,...
| | | +���- Month (1�12) or Jan, Feb,...
| | +����-� Day of month (1�31)
| +������� Hour (0�23)
+��������- Minute (0�59)
I'd be really tempted to run using the #weekly keyword if you don't care what time of day this is run. It should run every Sunday, and is definitely more readable.
#weekly some_script.sh

Cron Expression (Quartz) for a program to run every midnight at 12 am

What is the cron expression in Quartz Scheduler to run a program at 12 am every midnight GMT.
I have never used quartz before so I am still learning.
Is the expression 0 0 12 * * ? or is that for 12 pm (noon). Could anyone tell me?
1 Seconds
2 Minutes
3 Hours
4 Day-of-Month
5 Month
6 Day-of-Week
7 Year (optional field)
So in your case:
0 0 0 * * ?
This will fire at midnight, if you want to fire at noon:
0 0 12 * * ?
Or both:
0 0 0,12 * * ?
A good page if you want to get more complicated: http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-06
Have an awesome day!
<Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week>
The following graph shows what it consists of:
* * * * * *
| | | | | |
| | | | | +-- Year (range: 1900-3000)
| | | | +---- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month (range: 1-31)
| +---------- Hour (range: 0-23)
+------------ Minute (range: 0-59)
Cron Expression for a program to run every midnight at 12 am.
0 0 0 1/1 * ? *
A great website to create your own Cron Expression easily without much knowledge of Cron Expression : Cron Maker
It will help you build your own cron expression and show you the next firing date times of your cron like this.
1. Wednesday, July 6, 2016 12:00 AM
2. Thursday, July 7, 2016 12:00 AM
3. Friday, July 8, 2016 12:00 AM
4. Saturday, July 9, 2016 12:00 AM
5. Sunday, July 10, 2016 12:00 AM .....
Cron Expression for a program to run every midnight at 12 am should be
0 0 0 * * *

Resources