Why Crontab Uses or When Both Day of Month and Day of Week Specified

Does crontab day of week and day of month must both be matched at the same time?

It means if one of the two conditions is satisfied (can be both, does not need to be)

Note: The day of a command's execution can be specified in the following two fields — 'day of month', and 'day of week'. If both fields are restricted (i.e., do not contain the * character), the command will be run when
either field matches the current time. For example,
30 4 1,15 * 5 would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

source: man 5 crontab

In the examples of the OP, if the day is Wednesday Jan 09 2019, than the crontab:

x x 9 x 2

will execute because it is the nineth day of the month, and

x x 10 x 3

will execute because it is a Wednesday

CRON with AND relationship between mday and wday

I guess, instead of specifying the wday (Friday=5), you'll just have to specify the months where the 13th is a Friday; so, for 2019:

0 0 13 9,12 * : do stuff, but avoid black cats

Or, eternally more elegant, create a small script:

$> cat /home/me/bin/test_friday_13
#!/bin/bash
if [ "$(date +'%A %d')" != "Friday 13" ]
then
exit 1
else
: do stuff, but avoid black cats
exit 0
fi

and make the crontab entry:

0 0 13 * * /home/me/bin/test_friday_13

The script approach has the added benefit that you can run it from the command line. (Note: do not forget to alter the weekday name in the script to reflect your environment.)

Crontab Day of the Week syntax

0 and 7 both stand for Sunday, you can use the one you want, so writing 0-6 or 1-7 has the same result.

Also, as suggested by @Henrik, it is possible to replace numbers by shortened name of days, such as MON, THU, etc:

0 - Sun      Sunday
1 - Mon Monday
2 - Tue Tuesday
3 - Wed Wednesday
4 - Thu Thursday
5 - Fri Friday
6 - Sat Saturday
7 - Sun Sunday

Graphically, * * * * * command to be executed stands for:































minutehourday of monthmonthday of week
(0-59)(0-23)(1-31)(1-12)(1-7)
*****command to be executed

crontab (cronie), run certain days of the month

From crontab manual:

Note: The day of a command's execution can be specified by two
fields — day of month, and day of week. If both fields are restricted
(i.e., aren't *), the command will be run when either field
matches the current time. For example,
``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however,
achieve the desired result by adding a test to the command (see
the last example in EXAMPLE CRON FILE below).

The mentioned example looks like this:

0 4 8-14 * *    test $(date +\%u) -eq 6 && echo "2nd Saturday"

So in your case it would be like this (not tested):

45   8,12,16 4-31 * *  test $(date +\%u) -lt 6 && my_program

crontab week of the day not working - it send everyday

This is the correct, documented behavior. From man 5 crontab on Ubuntu 16.04 (emphasis mine):

Note: The day of a command's execution can be specified by two fields — day of month, and day
of week. If both fields are restricted (i.e., aren't *), the command will be run when either
field matches the current time.
For example,
30 4 1,15 * 5 would cause a command to be run at 4:30 am on the 1st and 15th of each
month, plus every Friday. One can, however, achieve the desired result by adding a test to the
command (see the last example in EXAMPLE CRON FILE below).

Later in the man page they give an example that does just the sort of thing you want:

   # Run on every second Saturday of the month
0 4 8-14 * * test $(date +\%u) -eq 6 && echo "2nd Saturday"


Related Topics



Leave a reply



Submit