How to run crontab job every week on Sunday - linux

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

Related

Linux crontab for nth Satuday of the month

I like to run back up for all weekdays except Saturday.
My crontab entry
30 16 * * 1,2,3,4,5 ./backup.sh
This entry working fine.
Also, I like to take back up on 1st, 3rd Saturday.
If any 5th Sutarday available in a month then the back up should run. What will be the entry for crontab? I am guessing
30 16 1-7, 15-21, 29-31 * 6 ./backup.sh
Am I right?
Am I right?
No you are not correct. The crontab manual states:
Note: The day of a command's execution can be specified in the following two fields day of month, and day of week. If both fields are restricted (i.e., do not contain the "*" character), the command will be run when either field matches the current time. For example,
30 4 1,15 * 5 would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
So how can we do it?
If you want to determine which Saturday of the month it is, i.e. whether it is the 1st, 2nd or 3rd Saturday of the month, all you have to do is look at the weekday of the Saturday and do the following integer computation:
D=$(date "+%d")
echo $(( (D-1)/7 + 1 ))
This value will return the corresponding number. This does not only work for Saturdays but for any Weekday.
Since the OP wants the cron to work on the 1st, 3rd and potentially the 5th Saturday, it actually states that the cron runs on every odd-numbered Saturday:
D=$(date "+%d")
echo $(( ((D-1)/7 + 1) % 2 ))
Using this as an additional test, allows us to write the cron as:
# Example of job definition:
# .---------------- 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
30 16 * * 6 (( (($(date "+\%d") - 1)/7 + 1) % 2 == 1 )) && command

Cron Job to run on a range of days only on a particular day of week

0 0 2-31 * sun /home/ubuntu/x.h
0 0 2-31 * mon-sat /home/ubuntu/y.h
This ends up running both of them. Am I doing something wrong here?
This is the crontab format:
* * * * *
| | | | |
| | | | +---- Day of the Week (range: 0-6, 0 standing for Sunday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month (range: 1-31)
| +---------- Hour (range: 0-23)
+------------ Minute (range: 0-59)
Ubuntu man 5 crontab says:
field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)
So, this should work for you:
0 0 2-31 * 0 /home/ubuntu/x.h
0 0 2-31 * 1-6 /home/ubuntu/y.h
I'm not sure why 7 would run on Saturday--is your system time accurate and in the right timezone?
Edit: Ah, yes, unfortunately you cannot specify both the day of the week and the day of the month. From man 5 crontab:
Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example, ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).
So, the answer is:
0 0 2-31 * * test $(date +\%u) -eq 7 && /home/ubuntu/x.h
0 0 2-31 * * test $(date +\%u) -ne 7 && /home/ubuntu/y.h
$(date '+%u') returns 1-7 representing Monday thru Sunday. Try echo $(date '+%u') for an example.
from datetime import datetime
from datetime import timedelta
import urllib.request
//urls to hit
urls=["https//:example1.com","https//:example2.com"
]
//function to hit url
def call(url):
urllib.request.urlopen(url)
//function to get date
def get_month_diff(current,nom):
m1= current
m2=m1 - timedelta(days=nom*30)
m3=current
m4=m2.replace(day=1)
m5=m3.replace(day=1)-timedelta(days=1)
list=str(m4).split(" ")[0].split("-")
list.reverse()
startDate="-".join(list)
list1=str(m5).split(" ")[0].split("-")
list1.reverse()
endDate="-".join(list1)
for i in range (0,5):
call(urls[i]+""+startDate+"/"+endDate)
//main execution function
def solve():
month=str(datetime.today()).split("-")[1]
if month in ["01","04","07","10"] :get_month_diff(datetime.today(),3)
if month in ["01","07"]:get_month_diff(datetime.today(),6)
get_month_diff(datetime.today(),1)
solve()

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

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

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.

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