329

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
dev_fight
  • 3,345
  • 3
  • 14
  • 5

11 Answers11

579

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.

Bjoern Rennhak
  • 6,766
  • 1
  • 16
  • 21
  • 173
    To be more readable you can use one of `sun, mon, tue, wed, thu, fri, or sat` for the day. This also saves you from having to choose between using `0` or `7` for sunday. – flu May 15 '14 at 13:15
244

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.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 15
    Just to help others avoid the silly mistake I have just made, and make sure you set the minute to something other than * or it will execute on every minute of that hour! – user2924019 Jul 27 '16 at 08:24
45

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
Dmitry
  • 877
  • 1
  • 16
  • 30
xShirase
  • 11,975
  • 4
  • 53
  • 85
  • 1
    Voting up for mentioning how to specify the command to run each time. (The user column, however, needs to be omitted when editing via the "crontab" command.) – Joachim Wagner Jan 03 '18 at 15:09
  • 2
    Sunday midnight is the 0 hour of Monday, i.e. `0 0 * * 1`. – Fred Loney Sep 28 '18 at 18:08
  • @FredLoney thanks for pointing that out. "Astronomers and the military use a system in which midnight is 0 hours. In that system, tonight’s midnight is the first moment of tomorrow. But as for the rest of us – there’s no official answer. That’s why airlines always schedule flights for 11:59 p.m. or 12:01 a.m. – never midnight." via https://earthsky.org/space/edit-time – PJ Brunet Sep 16 '20 at 06:54
  • Just want to point out a potential "gotcha" is that by default cronjobs are scheduled in UTC time (4 hours ahead of east coast time) – dgg Aug 10 '22 at 19:11
27

The crontab website gives the real time results display: https://crontab.guru/#5_8_*_*_0

enter image description here

TechPassionate
  • 1,547
  • 1
  • 11
  • 18
6

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

Naik Ashwini
  • 750
  • 12
  • 32
5

I think you would like this interactive website, which often helps me build complex Crontab directives: https://crontab.guru/

jasper
  • 123
  • 2
  • 10
3

Cron job expression in a human-readable way crontab builder

Vytautas
  • 77
  • 4
  • 1
    Imho, this is not an answer as it does not add anything here, nor does it answer the question here in any way. – Pankaj Jun 07 '19 at 03:18
2

@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

sixsixsix
  • 1,768
  • 21
  • 19
1

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. 
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Bachan Joseph
  • 347
  • 4
  • 5
  • 2
    Congratulations on your first answer at StackOverflow! Please be sure to check [Answering Guide](https://stackoverflow.com/help/how-to-answer). For instance, answer typically should has some new information that is missing in existing answers. – doz10us Oct 13 '17 at 11:28
  • 9
    this will run 24 times on sunday, 10 minutes past the hour every hour. – Jens Timmerman Nov 10 '17 at 12:14
1
* * * * 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)
0

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