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.
Related
This is more difficult than I thought. In the month of September 2022, I need cronjobs to run on the 1st, 3rd and 5th Thursdays (there are 5 Thursdays this month!), but in October 2022 I need them to run on the 2nd and 4th Thursdays.
I would like my tasks to run every 2 weeks without skipping any days when a month has 5 Thursdays
How can I accomplish something like this with a cronjob? Is it even possible?
You can do cron on odd/even week of month.
#Every day except Thursday at 1am
0 1 * * 0,1,2,3,5,6 yourCommand
#Every Thursdays at 1am, proceeds only on even weeks
0 1 * * 4 test $((10#$(date +\%W)\%2)) -eq 0 && yourCommand
#Every Thursdays at 6am, proceeds only on odd weeks
0 6 * * 4 test $((10#$(date +\%W)\%2)) -eq 1 && yourCommand
If you want more info regarding this please go this reference link :
cron_info
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
I'm trying to write a crontab expression that will begin a specified period of time and run on an interval for a 24 hour period. For example I want the job to run every Thursday beginning at 4 PM and repeat every hour for 1 day. Is there a way to do this? Everything I have tried stops at the end of the day Thursday.
You need two crontab entries, one for the occurrences on Thursday and one for the occurrences on Friday.
For example (I have not tested this):
0 16-23 * * 4 your_command
0 0-15 * * 5 your_command
The fifth column is the day of the week, with Sunday=0. (Vixie cron also lets you specify the day of the week by name.)
I need a cron expression that will fire every second day excluding weekends.
Example:
The schedule starts on Monday. The schedule continues in the following manner:
(1st week) Monday>Wednesday>Friday
(2nd week) Tuesday>Thursday
(3rd week) Monday>Wednesday>Friday
(4th week) Tuesday>Thursday
Is that possible using only cron? I know a solution would be to run it every day and when it runs on weekend 'manually' prevent it from running.
Maybe something like could help...
* * 1-31/2 * mon-fri command.sh
That means, "At every minute on every 2nd day-of-month from 1 through 31 and on every day-of-week from Monday through Friday."
https://crontab.guru/#__1-31/2_*_mon-fri
http://corntab.com/?c=__1-31/2_*_MON-FRI
(Didn't tried on real machine)
I will consider extended expression format so your query will looks like:
S M H DoM M DoW Y
0 0 10 1-31 * 1#1,3#1,5#1 *
This query can be understood as: Repeat at 10:00:00 every day of every month where day of week is (monday, wednesday, friday) and it's first week of month.
You would define such 4 queries (i'm considering that 1 in 1#3 is just monday and 3 is week number in month):
1.) 0 0 10 1-31 * 1#1,3#1,5#1 *
2.) 0 0 10 1-31 * 2#2,4#2 *
3.) 0 0 10 1-31 * 1#3,3#3,5#3 *
4.) 0 0 10 1-31 * 2#4,4#4 *
which runs the same command. But it won't work becouse of limitations of most of evaluators (as i guess).
If you are familiar with .NET, I made evaluator which handle such expressions correctly, but it's only evaluator so what you only receive are dates when your event should occur. There is no job sheduler integrated with it. Click
i want to set a cronjob in directadmin control panel and i have a question. if i set a job in this format:
05 21 * * * /home/backup.sh
my script will run only one time in a day at 21:05 OR every 5 miutes(12 times in an hour) and every day at 21:00 ?? i want to my cronjobs run's only one time in a day at 21:05! please help me
Your script will run at 21:50 every day.
See the file formats manpage for crontab:
$ man 5 crontab
The line parts before the command for your crontab are: (Below is from the manpage.)
The time and date fields are:
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)
A field may be an asterisk (*), which always stands for "first-last".
And you will see this example even further below: (Below is also from the manpage.)
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
man is your friend.
As per the cronjob set by you the first * means minutes, second * means Hour, third * means month, fourth * means day of the month and last * means day of the week. So if you set by
5 21 * * * it would run the job at 9:05pm minutes only.
For more about cronjob check http://www.thesitewizard.com/general/set-cron-job.shtml
Thanks & Regards,
Alok Thaker