Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Automating recurring tasks with cronjobs

Monday, March 9th, 2015 by Servage

cronjobWebsites often consist of information in a database that is shown to users in combination with design and layout, to provide a good representation of the content. For example a blog has posts, comments and author information in a database, which is displayed to visitors via a template through a browser. Systems like WordPress provide a great foundation for such sites, because it provides the entire backend infrastructure for the content management, and at the same time offers great flexibility for customizing the visual output. Another example is an online store, which holds product information in a database, while showing it to customers on the website with a given design desired by the vendor. Furthermore the database is used to store orders, invoices and other relevant data.

Regardless of the information stored in a database, there will likely be some need to handle recurring operations with that data. In case of a blog, you may wish to automatically publish information based on date & time, or in case of an online store, you may want to clear old orders, send newsletters or process data for some other purpose at given intervals. The possibilities are endless, but the common issue is that you want to automate recurring tasks on a regular basis. This is where cronjobs come into play as a great tool to help with that.

What are cronjobs?

Cronjobs are specific commands that should be executed at a given time interval. For your website this can be setup as specific paths to access, which triggers a script to perform the desired operation.

For example you could say that /path/to/my/command.php should run every hour, or every day at 06:00, or whatever interval suits your purpose. The script should ideally be a non-public one which is only used internally for your command. However, one of the benefits of running these commands via your script code, is that you can build them just like any other web based PHP script you use in your system. Thereby you can benefit from existing code or the framework you use for your website, making it very easy to build useful commands quickly.

Use-cases for cronjobs

Below are a list of use-cases for cronjobs. They are inspiration for automation of existing functionality you currently may be running manually, or new functions you could implement with automatic execution.

You can setup cronjobs in the Servage control panel via Tools > Cronjobs > Create new cronjob.

  • Calculate statistics and send summary email to administrator.
  • Publish pending content that has been prepared beforehand.
  • Initiate and stop marketing campaigns and offers at specific times.
  • Process and filter data on a regular interval to cleanup or analyze.
  • Send reminders for potential actions based on data status.

You can see above that many good features can be aided by regular automatic script execution, and it relieves administrators from performing such tasks manually. Setting up cronjobs thereby reduces the overhead while running a web application.

Compartmentalize scripts

When using cronjobs you don’t really see the script being executed, thus debugging may be hard if you don’t implement good logging and reporting functionality. It is recommended that you combine detailed logs with compartmentalized code structure, so you  can easily identify and solve problems if they occur. For example, avoid loading all unpaid invoices from the database and process email reminders in one step. Load them individually and process them step-by-step, so you have a better overview of which specific data record or script step is not working as intended. Status fields in the database are a good help, as they can be used to flag success or problems with the automated tasks and their progress.

Automating recurring tasks with cronjobs, 5.0 out of 5 based on 5 ratings
Categories: Tips & Tricks

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.