bash

Cron every last Friday of month

(Last Updated On: 17 January, 2018)

We wanted to run a script every last Friday of the month. The script was set to remind people about certain issues. Friday is a nice day to be reminded of things, right?

Anyway, by putting this in the /etc/crontab file, one can achieve the request:

59 11 * * 5 username [ $(date +"\%m") -ne $(date -d 7days +"\%m") ] && $(cd /to/folder/; php myphpscript.php)

Which says:

At 11:59 on Friday run this command:

[ $(date +"\%m") -ne $(date -d 7days +"\%m") ] && $(cd /to/folder/; php myphpscript.php)

The first part:
[ $(date +"\%m") -ne $(date -d 7days +"\%m") ]

will test if the current month is equal to the month that will be in 7 days. If it is, it will generate something like:

01 -ne 01

which means it is current date is in January month (01), and it is compared (-ne = not equal to) the date in 7 days. If the result is:

01 -ne 02

one knows that the month in 7 days is February, hence the current date is the last Friday of this month 🙂

Reference: http://techsk.blogspot.no/2008/06/how-to-run-cronjob-on-last-friday-of.html

 

Leave a Reply

Your email address will not be published. Required fields are marked *