Executing a Monthly cron.monthly on a Specific Date

Unix Systems

Cron is a powerful tool in linux-based operating systems to execute jobs at specified intervals. This easy hack makes monthly cron jobs execute at a specific date.

The easiest way to execute a script as a scheduled job is to put your script executable file on cron folders:

  • /etc/cron.hourly
  • /etc/cron.daily
  • /etc/cron.weekly
  • /etc/cron.monthly

And you’re all set. The only issue with this is that cron manages it arbitrarily. This means the user does not have a way to tell cron to execute it on a specific time or day.

However, for longer intervals (i.e. weekly or monthly), we as users sometimes need to specify a day or date of execution. For example, if you need to automatically renew subscriptions, you would like to execute it at the start of the month. For this type of task, you would need to dwell on another tool–crontab–which is a little bit more complex. You may have experienced that you would need to browse on your search engine to learn again how to jobs on crontab.

Configuring Monthly Cron Jobs without using Crontab

Luckily, there’s an easier way to do it, with a simple hack:

When cron executes, it stores the last execution on a file in a folder:

  • /var/spool/anacron/

Say, we want to execute monthly jobs on the first day of the month, then we just need to edit the file:

  • /var/spool/anacron/cron.monthly

Make sure that you edit it to a date that is sometime in the past (as it is a storage of last execution). So for example, if the file contains 20180928 you need to edit is as 20180901 (YYYYMMdd is the convention).

When cron checks for this file in the succeeding month, it will see that the last execution was on the first day. So from then on, it will always execute at the start of the month.

Enjoy!


Do you need to monitor if your cron jobs executed successfully? You can use PushMon to do this. Go to our Sample Code page to learn how. With our forever-free plan, you can add up to 3 URLs to monitor your cron jobs.