I am new to LINUX. I have a clear idea on how cron works, but suddenly a small doubt struck at my mind. Suppose if I want to execute ls command on 5th march 5.30AM, then my cron command will be 30 05 05 03 ?? ls.
My simple question is that what entry will come on ?? place. And suppose if I enter 01 in that position instead of ?? , what will happen. Please excuse me if this is a simple question and please help me solve the same.
In your case
30 05 05 03 * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
?? says that you don't mind the day of the week because you are already stating the exact date. If you place the 01 it would still run but its redundant.
The character * means "any value" in the crontab file so that's what you'd use.
If you used 1, your job would only execute on the conditions you'd already specified AND it was a Monday.
That entry is for the day of the week you want it to run on. If you put 01 there then it will only run at 5:30 on March 5ths that happen to be a Monday. If you want to specify "any day of the week" then use *.
You'd enter * which means "Anything is OK here." That field is the day-of-week, and you don't care if it's a Monday or Tuesday etc. If you enter 01 you will get it to run only in years when that date is a Monday.
Related
I want to execute a crontab every first of month and every sunday, Here is what I think to do, I am not sure if it will execute it the first of month only if it's a sunday or every first of month and every sunday, Any ideas to clarify this are welcome:)
00 16 1 * 7 "command"
Thanks in advance
You are correct, Kahina.
00 16 1 * 7 "command"
will run the "command" at 16:00 on the 1st of each month, plus every Sunday.
I have an agenda job that I want to schedule for every five months. Suppose I started that job on Jan 20th, so now the schedule should be Jan 20th 2019, June 20th 2019, Nov 20th 2019, April 20th 2020 and so on.
Agenda uses cron for scheduling.
The problem with 00 00 20 1,6,11 * is that it will never run in April, this will run
at 2019-06-01 00:00:00
then at 2019-11-01 00:00:00
then at 2020-01-01 00:00:00
then at 2020-06-01 00:00:00
then at 2020-11-01 00:00:00.
Another expression that I used is 00 00 20 */5 *. The next run times are
at 2019-06-20 00:00:00
then at 2019-11-20 00:00:00
then at 2020-01-20 00:00:00
then at 2020-06-20 00:00:00
then at 2020-11-20 00:00:00
but they are not the month that I want it to run, i.e., at a regular interval of five months.
I couldn't find a way to start on the exact date in the month when it's started. If it is ok it's the first (like example below), you can use this:
Contab guru
βAt 00:00 on day-of-month 1 in every 5th month.β
Otherwise play with the values to suit your needs.
Cron can't do this directly, but you can move that logic into a script that you call every month. Your cron job could look like
0 0 20 * * monthcheck 2019 1 5
and monthcheck is a script somewhere in your path with this content:
#!/usr/bin/env bash
baseyear=$1
basemonth=$2
interval=$3
read -r year month <<< "$(date '+%Y %-m')"
if (( (12*(year-baseyear) + (month-basemonth)) % interval == 0 )); then
echo "Run the script"
fi
This takes a base date specified by year and month (baseyear and basemonth, supplied as arguments with 2019 and 1 in the crontab entry) plus an interval in months (interval, value 5 in the example).
It then reads the current year and month into year and month, checks how many months have passed since the basedate, and if that difference is a multiple of interval (modulo division by the interval is 0), then it runs your script (just an echo command in this example).
The formatting string for date uses %-m to avoid zero padding for the month (gets 1 instead of 01). This feature might not be present in every date β I'm using GNU date here.
I have to calculate the time until a machine is available again.
The work time is daily from 06:00am until 22:00pm.
If I simply add the time with the hours, the result looks like this.
| Job Duration (hours) | Start | End |
| 18,75 | 21.09.2017 06:00 | 22.09.2017 06:15 |
| 20,14 | 21.09.2017 11:30 | 22.09.2017 07:38 |
This is wrong for me, because the work time is only from 06:00am to 22:00pm.
I would like to achieve the following result:
| Job Duration (hours) | Start | End |
| 18,75 | 21.09.2017 06:00 | 22.09.2017 07:45 |
| 20,14 | 21.09.2017 11:30 | 22.09.2017 15:38 |
Thanks in advance.
Have a look at this example
To get your End time I calculate the actual time the job takes i.e non-working and working hours.
There's 16 hours in the working day (between 6 and 22) and there's 8 non-working hours (between 22 and 6).
=8*(INT(A2/16)+IF(60*((A2-INT(A2/16)*16)+HOUR(B2))+MINUTE(B2)>60*22,1,0))+A2
To break this formula down this:
Calculates the number of non-working periods by doing INT(A2/16)*8 This divides the number of working hours into the job duration
I Then test if the job duration is going to take longer then a day using IF(60*((A6-INT(A6/16)*16)+HOUR(B6))+MINUTE(B6)>60*22 This calculates whether the job would go past 10PM. I compare the two in minutes instead of hours for accuracy. If the job would go past 10PM I add on another non-working period
I then multiple the total number of non-working periods by the number of non-working hours (8)
And finally add the total of non-working hours onto the original job duration
All in all this calculates the amount of non-working hours + working hours to give me the actual time
I then add the actual time to the start time using:
=B2+C2/24
to get my end time.
Then finally, I've combined the formulas to give you your answer in one column
=B2+(8*(INT(A2/16)+IF(60*((A2-INT(A2/16)*16)+HOUR(B2))+MINUTE(B2)>60*22,1,0))+A2)/24
Giving your results:
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.
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.