0

I tried for the Weekend (SAT-SUN) cron expression:

0 0 18 ? 1/1 SAT#1,SUN#1 *  

but it didn't work.

3

1 Answer 1

2

You don't say which CRON version you are using but from my limited experience generally the CRON Configuration is -

Min Hour Day Month Day-of-Week followed by your command to execute

so on the first day of month this would be 0 0 1 * * not starting 0 0 18

One solution is that you could use the syntax of the bash case... esac statement

0 0 1,2,3 * * case `date "+\%w\%d"` in \\[1-5]\\01|\\1\\02|\\1\\03) /path/to/your/script ;; esac 

The cron entry is then checked against the "case word in" value of the expression until a match is found. If nothing matches, the default condition will be used.

Bash Basic date format $date “+ Where Parameter from the list below and separator is a any field separator like hyphen( – ) , slash , colon : and it is optional.

Bash Date Format Examples %d is keyword for two digit date %w day of week starting with Sunday (0), i.e. smtwtfs (note sunday can be both 0 and 7).

Here the string word is compared against every pattern until a match is found. The statement(s) following the matching pattern executes. If no matches are found, the case statement exits without performing any action.

When statement(s) part executes, the command ;; indicates that flow should jump to the end of the entire case statement.

so if the date is 01 on a monday to friday or (day 1) monday is the date 02 or 03 of month then run your script at 00:00 hrs otherwise do nothing

or for the weekend

0 0 1 * * case `date "+\%w\%d"` in \\0\\01|\\6\\01|\\6\\02|\\6\\03|\\6\\04|\\6\\05|\\6\\06|\\6\\07) /path/to/your/script ;; esac

Otherwise but not all CRON work the same you could also try the following options.

For Every Saturday OR Sunday if it is the 1st to 7th

0 0 1-7 * 6,7

0 0 1-7 * SAT,SUN

First Weekday of Month

0 0 1W * *  

First Saturday OR Sunday if it is the 1st to 7th

0 0 1-7 * 6#1,7#1   

0 0 1-7 * SAT#1,SUN#1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.