Ubuntu cron job every workday

13,504

Solution 1

These syntaxes are valid for all working days a 8:00 AM :

  • 0 8 * * 1-5 /path/to/command >/dev/null 2>&1
  • 0 8 * * 1,2,3,4,5 /path/to/command >/dev/null 2>&1 as you said @aleksandar-pavić

More explanations with these links :

The use of >/dev/null 2>&1 is optional, the goal is to redirect all the outputs to /dev/null.

You must have another problem, you must also specify the user if you use crontab -e

Here is a reminder of the cron syntax

* * * * * *
| | | | | | 
| | | | | +-- Year              (range: 1900-3000)
| | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month  (range: 1-31)
| +---------- Hour              (range: 0-23)
+------------ Minute            (range: 0-59)

Solution 2

Finally tested and found the answer, so 1-5, or MON-FRI does not work, what works is

0 8 * * 1,2,3,4,5 /path/to/command

Solution 3

First of all, it looks like you want to run the command on weekdays, as opposed to workdays.

weekday: any day of the week except Sunday or, often, Saturday and Sunday.

workday: a day on which work is done; working day.

If you wanted workdays, you'd have to schedule a script every day with cron, and then build into that script the logic necessary to determine whether "today" is a working day and the command needs to be executed, or not.

Since the question is tagged ubuntu-16.04, I had a look at the relevant man page which reads:

day of week 0-7 (0 or 7 is Sun, or use names)

Names can also be used for the month and day of week fields. Use the first three letters of the particular day or month (case doesn't matter). Ranges or lists of names are not allowed.

and then tried this crontab on a Ubuntu 16.04 server:

  *  *   *   * 1-5   date >> /tmp/date.txt

It does yield the desired output:

Mon May  1 00:00:25 CEST 2017
Tue May  2 00:00:03 CEST 2017
Wed May  3 00:00:47 CEST 2017
Thu May  4 00:00:01 CEST 2017
Fri May  5 00:00:53 CEST 2017
Fri May  5 00:01:01 CEST 2017

This is obtained by setting the system time at 00:00:00 subsequently for each day of the first week of May 2017. The results also highlight that cron does not guarantee execution with a precision interval smaller than one minute (if interested, see a question on this topic).

Just out of curiosity, I the tried the following crontab:

 *  *   *   * MON-FRI   date >> /tmp/date.txt

Surprisingly, and contrary to what the documentation says, it does still work:

Mon May  1 00:00:44 CEST 2017
Tue May  2 00:00:39 CEST 2017
Wed May  3 00:00:47 CEST 2017
Thu May  4 00:00:17 CEST 2017
Fri May  5 00:00:10 CEST 2017
Share:
13,504

Related videos on Youtube

Aleksandar Pavić
Author by

Aleksandar Pavić

Redmine Cookbook author. Visit blog! http://www.redminecookbook.com Follow me on Linkedin for smart IT related stuff! enter link description here IT Product and Service Development Specialist. Currently managing multiple projects, products and services used by thousands of business and individuals locally and worldwide, by utilizing Agile, SCRUM, ITIL, ISO 27001, SLA + own experience. Managed and managing IT projects with budgets from $0 to nine figures, and lasting from one day to years. Participating in EU funded IT projects like Exchange 3, Exchange 4, Sociotal, WeeLive and Clips. Developing cost-effective IT strategies for companies, cities, municipalities and startups. Redmine administrator and implementation specialist. (buy my book - www.redminecookbook.com) MsC in Product and Service development - industrial engineering BsC in IT, Software Engineering, Electrical engineering and computing, computing sicences and Management in IT: project management, procurement management, product management, infrastructure management, IT strategic management, business process development, IT security, redmine IT Development specialties: javascript, jquery, .NET, PHP, CSS, cakephp, symphony, MVC, vmware, ubuntu, apache, mysql, microsoft sql, C#, XML IT Administration: Netowrk security, firewall zones, antivirus, MSSQL, MSSCM, Linux, Ubuntu, Apache, Tomcat, MySQL, PostgreSql, VmWare, ProxMox, HyperV, Redmine, PHP, Cpanel, Plesk

Updated on September 18, 2022

Comments

  • Aleksandar Pavić
    Aleksandar Pavić over 1 year

    On Ubuntu 16, I'm trying to trigger cron job at 8:00 AM on workdays, and here is what I've tried so far

    0 8 * * 1-5 /path/to/command
    

    and

    0 8 * * MON-FRI /path/to/command
    

    Does not work