69

Hi I want to create a cron expression excluding saturday and sunday.

Tristan
  • 8,733
  • 7
  • 48
  • 96
Rakesh Sabbani
  • 1,655
  • 5
  • 17
  • 31
  • 3
    Did you read the `crontab(5)` man page e.g. http://linux.die.net/man/5/crontab ? – Basile Starynkevitch Feb 04 '12 at 19:50
  • 6
    I really don't see why this question 'is not on topic'. – carla Jul 18 '16 at 13:48
  • 3
    This is off topic because it has nothing to do with programming in the normal definition, but is more related to server administration. Therefore, https://serverfault.com/ is the proper place for this question (even if it's for a workstation crontab settings are still most often used on servers). One could loosely say it is programming the computer, but it's not programming as most people would define it. – J Roysdon Aug 21 '20 at 00:44
  • @JRoysdon thank goodness for your explanation. It can be very difficult on this site to deal with open-loop feedback, where something is deemed bad, but you have very little idea why it was deemed bad and therefore are unsure how to prevent a similar mistake in the future. In your case, you closed the loop with the feedback and it can be a good signal to future posters where the lines are between programming-specific and otherwise more computer-related questions not specific to programming, as well as where the more appropriate place to post is. Again, thank you. – Michael Plautz Aug 26 '20 at 04:42
  • @MichaelPlautz JRoysdon Your are both joking right ? Of course cron expression are about programming, just like regexp. You can't use CronTriggers from Quartz framework without knowledge about cron expressions : http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html I vote for reopen an obviously useful question. – Tristan Jan 14 '22 at 10:11
  • See similar "on-topic" question : https://stackoverflow.com/questions/9619362/running-a-cron-every-30-seconds/9619441 – Tristan Jan 14 '22 at 10:19
  • `cron` is a general scheduling syntax, used in many programming libraries, this seems on topic to me. – Danny Varod Jun 05 '23 at 11:10

3 Answers3

137

Begin the line with 0 0 * * 1,2,3,4,5 <user> <command>. The first fields are minutes and hours. In this case the command will run at midnight. The stars mean: for every day of the month, and for every month. The 1 to 5 specify the days. monday to friday. 6=saturday 0=sunday.

Michel
  • 2,523
  • 2
  • 17
  • 14
26

Try this:

# run every two hours at the top of the hour Monday through Friday
0 */2 * * mon-fri <command>
SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • 3
    "Use the first three letters of the particular day or month (case doesn't matter). **Ranges or lists of names are not allowed.**" – scribu Dec 14 '14 at 16:01
  • 5
    @scribu I guess it's relative to the cron daemon you use. In my case `dcron` which Slackware ships with does support this feature, it's even one of their examples. – SiegeX Dec 15 '14 at 16:39
  • https://crontab.guru/#*/10_*_*_*_1,2,3,4,5 it is very easy: “At every 10th minute on Monday, Tuesday, Wednesday, Thursday, and Friday.” – Bitfinicon May 07 '22 at 11:08
-1

To sum it up simples, 0 represents Sunday and 6 Saturday:

  • */10 * * * 1-5 - At every 10th minute on very day-of-week from Monday through Friday.
  • */10 * * * 0,6 - At every 10th minute on Sunday and Saturday.