crontab - Run a cron job on the first Monday of every month?

21
2014-04
  • Max Williams

    I'd like to run a job from cron at 8.30 on the first Monday of every month. The cron Wikipedia page says

    While normally the job is executed when the time/date specification fields all match the current time and date, there is one exception: if both "day of month" and "day of week" are restricted (not "*"), then either the "day of month" field (3) or the "day of week" field (5) must match the current day.

    (my emphasis)

    Does that mean I can't do the first Monday of the month, I can only do the first (or whatever) day of the month? I can't think of a way round it.

  • Answers
  • slhck

    You can put the condition into the actual crontab command:

    [ "$(date '+%a')" = "Mon" ] && echo "It's Monday"
    

    Now, if this condition is true on one of the first seven days in a month, you have its first Monday. Note that in the crontab, the percent-syntax needs to be escaped though:

    0   12  1-7 *   *   [ "$(date '+\%a')" = "Mon" ] && echo "It's Monday"
    

    Replace the echo command with the actual command you want to run. I found a similar approach too.

  • elemakil

    As far as I know it is NOT possible using only crontab, however one can use a wrapper function to pick the correct day from a "first seven days of month" contab entry; see this from entry.

    The wrapper script would be

    #! /usr/bin/ksh
    day=$(date +%d)
    if ((day <= 7)) ; then
       exec somecommand
    fi
    exit 1
    

    and you would need to run it (assuming it is called wrapper.sh and globally available) using the crontab entry

    0 0 * * 1 wrapper.sh
    
  • Indrek

    I have scheduled a job to run on the 4th Monday of every month at 4:00 PM as follows:

    0 16 22-28 * Mon [ "$(date '+\%a')" == "Mon" ] && touch /home/me/FourthMonOfMonth.txt
    
  • Manuel Gutierrez

    I made a general solution for this kind of problems, it works for first, second, third..... last weekday of the month.

    You can use it like this:

    30 06 * * Mon run-if-today 1 "Mon" && echo "First Monday"
    30 06 * * Thu run-if-today 3 "Thu" && echo "Third Thursday"
    30 06 * * Sun run-if-today L "Sun" && echo "Last Sunday"
    

    The script run-if-today check for both the weekday and the desired week date range, if both match then it returns 0, otherwise 1.

    Check the code here. https://github.com/xr09/cron-last-sunday


  • Related Question

    crontab - Run a command every week with Cron?
  • igul222

    Simple question- what would a crontab entry look like for a command I want to run every week on Saturday at 8:05 AM?


  • Related Answers
  • John T

    That should do it:

    5 8 * * 6 <user> <command>
    

    documentation (man 5 crontab):

                  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)
  • James Wald

    Sat 8:05AM run find

    # Minute   Hour   Day of Month       Month          Day of Week        Command    
    # (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
        5       8          *             *                Sat              /usr/bin/find
    
  • John T

    These answers are all correct, if you are unaware of how to use cron in the future, use one of many cron generators.

  • Dewayne Christensen

    From extensive investigation of "man 5 crontab", it looks like this'll do the trick:

    5 8 * * sat /usr/bin/man 5 crontab

  • Citizen
    5     8     *     *     6         *your command*
    
  • flypen

    You can also do like this:

    Change crontab:

    5 8    * * *   <user>   cd / && run-parts --report /etc/cron.daily
    

    And put all your command scripts in this directory /etc/cron.daily. Make sure that you have added the execution rights for them.