Cron job for running task every second week - cron

I want to create cron job for external application to run every second week starting from Thursday
Eg -
9:00 pm,23/5/2019
9:00 pm,6/6/2019
Cron Expression which I can modify
0 0 0 0 0 0 Minute | Hour | Day of Month | Month | Day of Week | Year
I already tried different combinations but not able to receive this functionality
Site I used
https://crontab.guru/
Cron Expression
0 0 0 0 0 0
Minute | Hour | Day of Month | Month | Day of Week | Year

There is no exact method to run the script every two weeks.
For alternatively you can use logic in the external application(script) to decide whether run the script or not. Then you can schedule the Cron to run in every week.
ex: 0 21 * * 4
Above Cron will run in every Thursday of week at 9 PM.
Please refer the below for more info.
https://www.systutorials.com/39652/how-to-run-a-cron-job-every-two-weeks-months-days/
From the script level you can decide whether to run the script or not

Related

Cron job one schedule for 3rd Sunday but different schedule every other day

We have jobs that are scheduled to run 1 time per day - every day
We do maintenance every 3rd Sunday of the month.
Up until now every month we have manually adjusted the cron to make the job run a little later in the morning then after maintenance we reset to the desired schedule
I am trying to change cron so that we
run at 7:00am every day EXCEPT the third Sunday of the month
run at 9:00am only on the third Sunday of the month
the second item I am able to handle
0 13 15-21 * 0
however, the first has me stumped. I thought this would do the job but it will only execute this if the day is between 1-14 or 22-31 but what if the 15th is not Sunday - then it won't run.
0 11 1-14,22-31 * *
How do I tell cron to run a schedule EXCEPT the third Sunday of the month?
There is a large base of guidance on how to limit when a cron runs to a specific window but I haven't found much for how to EXCLUDE a cron from a specific window
******** UPDATE ********
I think I may have come up with an answer - not sure if it is the most efficient but
0 11 1-14,22-31 * 0
0 13 15-21 * 0
0 11 1-14,22-31 * 1-6
The above will
run at 11:00 UTC on Sunday if date is between 1-14 or 22-31
run at 13:00 UTC on Sunday if date is between 15-21 (3rd Sunday)
run at 11:00 UTC Monday through Saturday all month
If a cron job has different timing than others, then it best to just define it by itself rather than trying to combine, unless you put some code in your script to do what you actually want. Doing something in cron on some nth day of the month is a pretty well known problem. Most crontab man pages have this note:
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 crent 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 it does OR between the day of the week and the day of the month, not an AND. I don't who ever thought this was helpful, but that's the way it is. You can see solutions at:
Run every 2nd and 4th Saturday of the month
you need something like (this assumes cron runs /bin/sh):
[ `date +\%w` -eq 6 ] && <command>
on your cron job line, the above is would restrict to running only on Saturday.

quartz scheduler to run on specific day and time, run every hour and should continue from there

Is it possible to fire a job which will
start on a specific date and time
run every hour from 1:00
from there it should continue till next 3 months for all days
EX:
Start the job on Feb 12 2017, 1:00 AM, runs every hour i.e 2:00AM, 3:00AM
and continue till April 12 2017 for days
Assuming my current date is 10 Nov 2016
Any solution for unix cron is also fine
Below will schedule only on 12th of Feb, Mar and April 2017.
|------------------------------------------------------------------|
| Seconds | Minutes | Hours | DayOfMonth | Month | DayOfWeek | Year|
| | | | | | | |
| 0 | 0 | */1 | 12 | 2-4 | ? | 2017|
|------------------------------------------------------------------|
It can achieved using 3 cron jobs programatically.
First job:- to register the second cron job on the day it should start. Will be triggered(run) only once.
Second job:- to run every hour. Will be triggered multiple times
Third job:- to de-register the second cron job . Will be triggered only once.
Explanantion:-
when the program starts, schedule the first job and also the third job on appropriate dates respectively.
When the first job is ready to run, it will schedule the second job.
When the third job is ready to run, it will remove the second job.

can't get the right cron job timing

i'm trying to do some stuff automatically every 8 Weeks, so i had open a new user crontab like this one:
crontab -e
0 9 * */2 1-5 do_this_stuff
# do it every 2 month on monday till friday at 9:00 am
This should do the job every 2 month on monday till friday on 9:00 am, but i does not. It is doing the job evey week once. Don't get it. What i'm doing wrong?
Running System is a latest debian.
regarding http://wiki.ubuntuusers.de/Cron it should run fine
The Anwer is, cron can't do a job randomly on a random day in a month. I had to change my crontab to: 0 9 1 */2 * do_sm_stuff -- this runs every two Month always on the first Day in a Month
thank you Igor

How to set cron job for bi-weekly (twice a week)

How do I set a cron job to run twice a week?
I know how to set a cron job for every week:
0 0 * * 0
How about the following:
0 0 * * 1,4
This sets the day of week to Monday (1) and Thursday (4). You can choose any values 0–7 (both 0 and 7 are Sunday).
For a more readable crontab, you can also use names:
0 0 * * MON,THU
See also: How to instruct cron to execute a job every second week?
In reply to Elby question :
0 0 1,15 * *
This will set cronjob for (fortnight) 2 times in a Month i.e 1st day and 15th day of a month.
These answers are great, but I wanted to share a tool I found right after looking at this question and answers called crontab.guru. I'm not affiliated, I just thought it was a nice tool.
crontab.guru for 'At 00:00 on Monday and Thursday.'

how to set cronjob for 2 days? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
We would like to use a cronjob to create a database backup.
The backup should occur ones every two days. Can the following cron-entry be used?
0 0 2 * * * backup-command
If this is wrong please tell me the correct command for setting the cron for 2 days.
From here:
Cron also supports 'step' values.
A value of */2 in the dom field would
mean the command runs every two days
and likewise, */5 in the hours field
would mean the command runs every 5
hours. e.g.
* 12 10-16/2 * * root backup.sh
is the same as:
* 12 10,12,14,16 * * root backup.sh
So I think you want 0 0 */2 * *.
What you've got (0 0 2 * *) will run the command on the 2nd of the month at midnight, every month.
p.s. You seem to have an extra asterisk in your answer. The format should be:
minute hour dom month dow user cmd
So your extra asterisk would be in the user field. Is that intended?
Disclaimer: this is a copy of my deleted answer here. I do believe this question is more canonical, and my answer fits better here.
There are two ways this question can be interpreted.
I would like to schedule a cron job every second day of the month.
I would like to schedule a cron job every two days.
These are two completely different cases because of the number of days there are in a month.
I would like to schedule a cronjob every second day of the month.
For this we use the combination of defining a range and a step value:
man 5 crontab: Step values can be used in conjunction with ranges. Following a range with /<number> specifies skips of the
number's value through the range. For example, 0-23/2 can be used
in the 'hours' field to specify command execution for every other hour
(the alternative in the V7 standard is
0,2,4,6,8,10,12,14,16,18,20,22). Step values are also permitted
after an asterisk, so if specifying a job to be run every two hours,
you can use */2.
See the following examples:
# 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
0 0 */2 * * command1
0 0 2-31/2 * * command2
* * */2 * * command3
command1 will be executed at 00:00 on every odd-numbered day (default range with step 2, i.e. 1,3,5,7,...,31)
command2 will be executed at 00:00 on every even-numbered day (i.e. 2,4,6,8,...,30)
command3 is an often made mistake. This will run on every minute of every odd-numbered day.
Note: It should be understood that in this approach command1 will run on both January 31st and February 1st (2 consecutive days)
Note: It should be understood that in this approach command2 will skip both January 31st and February 1st and will run only 3 days later.
I would like to schedule a cronjob every second day (starting from day X)
Here it gets more interesting. The notes above already demonstrated the problem with months having an odd number of days.
The quick way to think would be to evaluate the day of the year:
% date -d "2018-01-31" '+%j'
031
% date -d "2018-02-01" '+%j'
032
and you could easily investigate if it is an odd or even number. However, When doing this, you have again a problem on December 31st and January 1st in a common year (365 days; leap years have 366 days). This can be resolved when you have a continuous counter from a given day. Enter UNIX time stamp, the total seconds since 1970-01-01 00:00:00 UTC.
% date -d "2018-12-31" '+%s %j'
1546214400 365
% date -d "2019-01-01" '+%s %j'
1546300800 001
% date -d "2019-12-31" '+%s %j'
1577750400 365
% date -d "2020-01-01" '+%s %j'
1577836800 001
For a cronjob to run command4 only every second day at 00:00 and command5 every third day at 00:00 starting from "2018-03-14", the crontab would look like this:
# 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
0 0 * * * daytestcmd 2 && command4
0 0 * * * daytestcmd 3 20180314 && command5
with daytestcmd defined as
#!/usr/bin/env bash
# get start time in seconds
start=$(date -d "${2:-#0}" '+%s')
# get current time in seconds
now=$(date '+%s')
# get the amount of days (86400 seconds per day)
days=$(( (now-start) /86400 ))
# set the modulo
modulo=$1
# do the test
(( days >= 0 )) && (( days % modulo == 0))
Remark: UNIX time is given in UTC. If your cron runs in a different time-zone which is influenced by daylight saving time, it is advisable not to run the command between 2 and 3 o'clock. This could skip the command or run the command twice (depending if the time jumps forward or backwards)
Remark: UNIX time is not influenced by leap seconds
If you want to use the crontab -e command, try this time pattern:
0 1 */2 * * command_to_execute
Or, you can try SetCronJob for a web based solution. You can easily create cron jobs every 2 days with the second creating row.

Resources