Run Cronjob every other week (fortnightly)

A customer  has a development server and production server. They wanted all the patches updated on the development server 1 week before being replicated on  the live server – although, of course there might be updates that come out within the 1 week period that may cause issues. My task was to set up the cron jobs to run every other week. Machines in question were Centos 6.4 (64 bit) on ESXi. There isn't an inbuilt way of doing this via cron so you have to be a bit creative.

The job required running "yum update" then restarting Apache afterwards,

Using the date() function in Centos were are able to calculate the week number (1 – 52).

 

The following cron entry will only run on an odd numbered week  at 7am on a Tuesday :

0 7 * * 2 root expr `date +\%W` \% 2 >> /dev/null && /usr/bin/yum update && /sbin/service httpd restart

 

The following cron entry will only run on an even numbered week at 7am on a Tuesday :

0 7 * * 2 root expr `date +\%W` \% 2 >> /dev/null || /usr/bin/yum update && /sbin/service httpd restart

 

The cron job using the date function along with modulo to see if there is a remainder left over. Therefore every even week will result in no remainder. The first "&&" or "||" determines if this runs on odd or even weeks.