When it comes to automation one of your first thoughts is usually Cron. With Cron you can schedule you tasks in many different ways and almost everything is possible, but some things can be tricky. For example, scheduling a job to run on every first Monday of a month. The following line should do the trick, or maybe not?
0 14 1-7 * Mon /bin/task
Reading this in the usual way this should produce: on 14:00 every 1st to 7th day of a month, if this day is a Monday, /bin/task will be executed. Wrong! Take a look at the documentation and you will understand the "correct" behaviour.
$ man 5 crontab ... 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 (ie, 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.
So the fields for day of month and day of week are linked in a OR fashion. When either of these conditions is present the job will be executed. To get Cron to do a AND linking with these fields you need to check the day of week in your script. One possible way to do this is this:
0 14 1-7 * * [[ $(date +\%a) = Mo ]] && /bin/task
The "[[…]]" is a shorthand for IF in bash. The “&&” ensures, that the remaining line is only executed if the condition evaluates to TRUE.
Die Kommentarfunktion wurde vom Besitzer dieses Blogs in diesem Eintrag deaktiviert.