use day of month and day of week both in cron expression - cron

I explored and found that in quartz cron expressions :
sec min hour day_of_month month day_of_week year,
we can provide either day_of_month or day_of_week, but not both of them, as it's not implemented yet.
I want to run the scheduler after every two weeks and on MONDAY, THURSDAY, FRIDAY at 12 pm , then how can I achieve this.
providing following cron expression won't work:
* * 12 1/14 * MON, THU, FRI *
because we can't provide both day_of_week and day_of_month.
So, please let me know if there exists any other way of doing it, some other library etc. And I do not want to handle it in business logic rather simply using the cronexpression should suffice my needs.

try this site https://crontab.guru/ it may help you..
or http://www.corntab.com/?c=0_12___1,4,5
and for your question this will be that expression
0 12 * * 1,4,5
and for your understanding :
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 7) (Sunday=0 or 7)
| | | | |
0 12 * * 1,4,5 command to be executed
Output:
“At 12:00 on Monday, Thursday, and Friday.”
next at 2016-12-15 12:00:00
then at 2016-12-16 12:00:00
then at 2016-12-19 12:00:00
then at 2016-12-22 12:00:00
then at 2016-12-23 12:00:00
.....

As far as I understood, you need such a query:
0 0 12 1-7,14-21 * MON,THU,FRI *
which means, that you will run your program from first to seven day of month, from fourteen to twenty one day of month but only if day of week is Monday, Thursday or Friday.
So the next occurence time will be:
2016-12-19T12:00:00+01:00
2017-01-02T12:00:00+01:00
2017-01-05T12:00:00+01:00
2017-01-06T12:00:00+01:00
....
the other question is, if the evaluator can handle such query correctly, you have to check it.

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

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