Limiting a Cron Job to a specific day of the week in Cloud Sites

The Cloud Sites control panel does not have a built in option to run a cron job only on a certain day of the week. There are cases where you would like a job to only run on a certain weekday such as a weekly mailout or a weekend maintenance script.

There is an option to run every few days, however this option, similar to the cron Linux option it calls to, would work by finding the remainder of the day of the month and dividing it by the number you provide then run the job if this remainder equals 1. So if this was set to run every 7 days and the first day of the month is a Wednesday, then the job will run on every Wednesday of the current month, however it may run on any other day of the week next month depending on which day of the week the next month started on.

In order to run a cron job on a certain day of the week, you can run a check at the beginning of the script to check the day of the week and exit if it is not the day that the job should run on.

The following line can be appended to the top of a bash script for it to check if the day of the week is Sunday and exit if it is not. That way you can run the script every day of the week and it will only actually complete on the day that you designate.

if [[ -z "`date '+%a' | grep Sun`" ]]; then exit; fi

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
No Comments Posted in Uncategorized

Leave a Reply

*

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>