It is very easy to get overwhelmed by the many responsibilities of web hosting, especially if you are managing a website (or several sites) all by yourself. Many of the tasks that demand your attention are repetitive and would be much easier on you if they could be automated. Fortunately, Linux and other Unix-like servers usually come with a feature that allows you to do just that.
A cron job is an automated task that is executed automatically at the times you specify in crontab, a simple settings file for the cron daemon (crond). Cron makes it easy to automate commands or even a string of commands without having to interact with the server.
If you have a dedicated server or VPS, you can access crontab from the command line and configure it manually. If, however, you have a shared hosting account, you may still be able to add tasks to crontab from your web host’s control panel. In this guide, we will explain both procedures.
Manually Editing Crontab
First, to edit crontab, you need to have root privileges. Once you are logged in as root, you should be able to edit the file with your preferred text editor (i.e. vi or nano). On some operating systems, you can also run the command “crontab -e”. Crontab is usually located at /etc/crontab.
You should see seven columns in the crontab file:
- m - minute
- h - hour
- dom - day of the month
- mon - month of the year
- dow - day of the week
- user - the user that will execute the command
- command - the command itself
To create a cron job, start a new line in the file and enter your information according to this pattern. For example, if you wanted to run a command called “checklogs” at 11:55 PM on weekdays, you would type the following:
55 23 * * 1,2,3,4,5 root /usr/bin/checklogs
Save the crontab file, and the next time crond reaches the time you specified, it will execute your command.
Using a Control Panel
Most web-based control panels such as cPanel or Webmin will still use crontab to run your cron job, but they will give you a user-friendly interface to create them. Using the above example with the free and open source Webmin, you would do the following:
- Click “create a new scheduled cron job”
- Enter the username you want to use
- Set the cron job to “active”
- Type in the command you want to execute (with full path)
- Under “when to execute”, you can either use general times like “hourly” or set a specific time using the numbers provided.
- Click “create”
Some Linux distributions, such as CentOS, have preset directories for running general cron jobs. These may include the following directories: cron.daily, cron.hourly, cron.weekly, and cron.monthly. Drop any script into one of these directories, and they will execute them according to the directory’s schedule.
Your crontab file will have a list of those directories and the times at which they execute. You are free to adjust them to suit your schedule. These entries will look like this:
17 * * * * root cd / && run-parts --report /etc/cron.hourly
Making Your Life Easier
Almost all web hosting companies will allow you to run cron jobs for you websites. You will then be able to automate everything from e-commerce billing to system vulnerability checks. Cron can save you valuable time and make your life easier.
Tavis J. Hampton is a published author and Linux system administrator. You can read more of his writing at TavisOnline.com.Tweet