Guest Post: Finding Your Feet with Cron Jobs

Article written by: Jerry Low
  • Hosting Guides
  • Jan 05, 2014

What is so amazing about computers? Most will say that it’s their calculation power; others will state that it’s their amazing capabilities for 3-D applications, rendering, design and gaming.

I’d say it is the Internet and automation capabilities.

There are 1,7 billion Internet users today and this number grew 300% since 2002. A lot of these people have web hosting accounts for their personal blogs, pages, forums, web sites, e-shops and so on. So if you are one of those people, think about how often you find yourself in need to repeat simple operations numerous times a week to keep your site clean, optimized and safe. If your answer is “often” and you are tired of this – here you’ll find a tip how to make your life a bit easier.

This article is about the cron – a unique automation utility for Unix based servers.

Cron daemon

To describe the concept of a cron job we should first of all consider a service running on a server. Most things executed on any computer are processes, browser being the most familiar one. If you open your browser and then go to your Task Manager, you will discover that it is running as a process.

Simply speaking process is an application, your computer is handling at the moment. The service, on the other hand, is a long running application, that is usually started automatically on system start up. It runs in the background, consuming as little resources as possible when it is not needed. When the time comes for it to perform an activity it “wakes up”, does what it must and then returns into sleeping mode? again. Examples of server processes are http service, MySQL service, ftp service and so on. On Unix servers (i.e. servers powered by Unix, Linux or FreeBSD) such services are usually called daemons.

The Cron Daemon is a special service capable of executing tasks at specific points in time without user’s intervention or control. It can be called an automatic scheduling machine. The name cron comes from the word chronograph . By using cron scripts, you can create sets of commands you want executed on a scheduled basis automatically.

Crontab and commands

Cron commands are stored in a special file. It is called crontab(which stands for cron table). The list of commands you can execute manually can be performed with a cron as well. Normally cron tasks include the following:

  • making backups;
  • any kind of file operations (deleting, copying, moving, renaming etc.);
  • sending and receiving updates emails;
  • installing software updates;
  • checking links on other websites;
  • optimize databases;
  • many more;

The mentioned crontab is a file that usually has the following content:

01 * * * * interpreter-1 script_path-1
02 4 * * * interpreter-2 script_path-2
22 4 * * 0 interpreter-3 script_path-3
42 4 1 * * interpreter-4 script_path-4

In the example above the first 5 items in each line are the time scheduling parameters. First number stands for the minute field, second – for the hour field, third – for the day field, fourth – for the month field, fifth – for the weekday field. The possible values and field descriptions are shown in the table below:

Field Value Description
minute 0-59 The exact minute that the command is executed.
hour 0-23 The hour of the day that the command is executed.
day 1-31 The day of the month that the command is executed.
month 1-12 The month of the year that the command is executed.
weekday 0-6 The day of the week that the command is executed. Sunday=0, Monday = 1, Tuesday = 2, and so forth.

Working with precise time and dates may be somewhat uncomfortable. Therefore, a special syntax is introduced: “*” – stands for any (so called wildcard symbol). If you want to configure the script to run at 15th minute of every hour you simply use the following sequence:

15 * * * *

If you want the script to run every 15 minutes you use the sequence like this:

15/* * * * *

The interpreter item in this example defines the shell or program you want to run the script with. For example, to a run a regular shell script daily with the bash shell, you use the following line:

* * 1/* * * /bin/bash /scripts/

Here “/scripts/” (script path from the cron tab file example) is an absolute path to the file your script is located in.

To edit crontab, use the command “crontab –e“in console. Editing crontabs in the console as well as using console itself is usually very inconvenient for regular users. Therefore, almost every hosting control panel contains a graphical user interface (GUI) for working with crontabs and configuring crons.

Crontab GUIs

The number of hosting control panels is quite large today, but there are a few most widely used by hosting companies due to their friendly interface and good integration with operating systems. Let’s take a look at how to configure the crontab in the two very popular panels: cPanel and DirectAdmin.

The GUIs of these two panels use the same concept as a regular cron table file. The time scheduling parameters remain the same, as well as their syntax. To specify months, days, hours and minutes, you still have to use numbers and the approach described earlier in the text. The interpreter and script path are defined in a separate field in these GUIs. This is also true for the email address for sending logs (available only for cPanel, while DirectAdmin will send the logs to the default e-mail address of the user). If you want no logs sent at all, you need to add the following command after the script path: “>/dev/null 2>&1“. DirectAdmin’s panel has a special button for this, as shown on the screenshots below.

cPanel Cron Job Menu

To get to the crontab GUI, find the Cron Job button in the home of your cPanel:
Figure 1: cPanel “Cron jobs” menu icon

cPanel Cron Jobs menu fields
Figure 2: cPanel Cron Jobs menu fields

DirectAdmin Cron Job Menu

Locate the Conjobs button to enter the DirectAdmin crontab configuration GUI:

DirectAdmin CronJobs
Figure 3: DirectAdmin Cronjobs menu

Cron scripts

When you know how to correctly configure the crontab scheduling, all you need to do to run a script. You can either write one yourself, using a regular text editor (like notepad on Windows), or you can search for it on the web. What’s good about cron scripts is that they can easily be modified and can be used with almost any software installed on your server. Writing your own scripts requires knowledge of the commands of the specific interpreter you are planning to use; thereby, before searching for specific instructions, decide which interpreter you’ll use. C shell is the most widely used interpreter, so if you are interested in creating your own scripts, then the best place to start is Linux Shell Scripting Tutorial.

Bottom Line

Automating tasks performed for a hosting account can save you a lot of time and ensure the safety of hosted data. One of the best and easiest ways to schedule autonomous execution of various actions is to use the cron job utility available on most Unix, Linux and FreeBSD servers.

Great advantage of cron jobs is the fact that tasks are performed without any intervention from the user’s side. Distinctive features of cron jobs include easiness of configuration, solid execution reliability and ability to use any installed interpreter for scripts. Additional comfort comes with the use of hosting control panels, as they provide GUIs for scheduling tasks. For more information on crons check the crontab manual page.

About Sergey Smirnov.

Smirnovi4 graduated from Ukrainian National Aerospace University in 2010 with Master’s degree in “Propulsion systems and power installations of spacecrafts”. Currently he enjoys working as a writer for the SiteValley official blog and occasionally assists with marketing tasks. Author likes extreme sports (skateboarding, snowboarding, parachuting and scuba diving particularly). Music of his choice is punk and indie rock. His free time he dedicates to reading news of IT industry and music. Two of Smirnovi4’s greatest weaknesses are Dr. Pepper and flight simulators.

Article by Jerry Low

Geek dad, SEO data junkie, investor, and founder of Web Hosting Secret Revealed. Jerry has been building Internet assets and making money online since 2004. He loves mindless doodling and trying new food.

Get connected: