Using Cron Jobs to Automate Mundane Maintenance and Administration Tasks

unix cron server terminalUNIX-based Web servers are more commonly used to service hosting plans then their Windows counterpart, primarily because UNIX is an open-source operating system that developers can modify and maintain without support from Microsoft (as is the case with Windows). This makes it easier for a hosting company’s IT specialists to perform server maintenance practices and correct issues when they arise. UNIX also offers the ability to automate server tasks on a scheduled basis using what are known as “Cron jobs.” Of course, these Cron jobs can only be set up by users with telnet (shell) access, so if you are using a managed shared hosting plan you may rarely have to request permission to set up Cron jobs. If  you are renting a dedicated or virtual private server you have the permission to perform any server task you’d like.

How Do Cron Jobs Work?

In essence, a Cron job is an automated function that is performed by a UNIX-based server using a simple line of code known as a crontab schedule that is fed into a native application on the server known as a Cron tab. It can perform any command that can be executed on a UNIX server manually, and you can even include specific parameters for the command. A Cron tab schedule line of code consists of six main fields that are not separated by commas – minute, hour, day, month, day of week, and command line to execute. The range of values for each of these fields is as follows:

minute: 0 to 59

hour: 0 to 23

date: 1 to 31

month: 1 to 12

day of week: 0 to 7 (0 or 7 can be used to represent Sunday, Monday is 1, Tuesday is 2 etc)

command line to execute: you can specify any command the server can perform, as well as any applicable parameters

However, some of these fields can be replaced with an asterisk to become a wildcard (i.e. – if you wanted to run a function on a daily basis you wouldn’t need to input the specific month or date, so those fields would be filled with an asterisk). A comma can also be used to insert more than one value into a field. An example to help you grasp the concept:

 “1 4 4, 6 7 * example/cronjob.pl”

The above Cron tab schedule can be decoded to mean that on the 1st minute of the 4th hour (4:01 AM) of the 4th AND 6th (hence the comma) day of the 7th month (July) the server would run the command “example/cronjob.pl.” If you wanted to modify the above concept schedule and make your task run every Friday at exactly 5 PM  (the 17th hour of the day) it would look like this:

“0  17 * * 5 example/cronjob.pl”

In the above example the date and month are not necessary values because it is a weekly task, so they’re replaced with asterisk.

What Can a Cron Job Do?

Virtually every function that a UNIX-based server can perform can be automated by a cron job. You could set one up to automatically retrieve a specific set of data from an application and then send it to a database at certain time of the day. You could also schedule one to delete unnecessary information on a routine basis. Since all administration tasks are simply server functions, cron jobs can eliminate the need to perform mundane, tedious maintenance procedures that are both boring and unnecessarily time-consuming. Overall, becoming proficient with Cron jobs will allow you to become more advanced in the field of web hosting, which will ultimately result in greater productivity and progress. Now that you know what a cron job is, what it can do, and the basics of how it works, you may want to continue your quest by researching various tasks that are beneficial to run on a scheduled basis. If you have a Windows hosting account, consider the following:

Cron Job Alternative for Windows Servers

Windows cron jobFortunately, there is a suitable, albeit unconventional alternative solution for Windows that works very similarly to Cron jobs. First, you would have to set up a webpage, that when visited, would request a specific function from the server. Then you could use a server monitoring service like PowerAdmin to schedule an HTTP call (an automated visit to the webpage you just set up), which would subsequently launch the automated function your page is designed to initiate.

Cheap Linux VPS