How to Schedule Tasks Using Cron on a Linux Server

How to Schedule Tasks Using Cron on a Linux Server

Cron provides a simple system for scheduling tasks, and it can run from the Unix or Linux command line, or you can set it up in just a matter of minutes using CPanel on your website.

In this tutorial, we’ll be covering both options, giving you the flexibility to run Cron tasks however you need to.

Using Cron from the Command Line

In Unix or Linux, Cron is set up as a daemon, which means it remains inactive until a specified time.

In most cases, the Cron daemon (crond) is automatically installed as part of your OS distribution, but just in case, we’ll first cover how to check that it’s working. The simplest way is to enter the following command:

# service crond status

The expected output would be something along the lines of:

crond (pid ####) is running...

If you don’t get a confirmation that crond is running, the application will need to be restarted manually. The following command will not only restart crond, it will also make sure that it’s configured to run at startup:

# chkconfig crond on

Now that crond is on, we can place useful scripts into its sub-directories. The sub-directories are located in /etc and they are /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly.

Each of these directories will run all of its scripts on an hourly, daily, weekly, or monthly basis. You simply need to insert the scripts you want it to use, and you’re all set.

Using Cron in cPanel

If you’re figured out how to use cron via the command line, you’ll find cPanel method a breeze.

The icon for Cron Jobs should be located on the main screen of your cPanel, under the Advanced header. Select it.

The next page will have an Add New Cron Job header, which will have options for Common Settings as well as the minute, hour, day, etc. intervals that we mentioned previously.Simply select the correct time interval, and in the Command field, enter the command or commands you wish the system to run.

cPanel Cron Job

You can also view Current Cron Jobs, which will let you edit or delete existing jobs.

Using Cron E-Mail Notifications

You may want to specify an email address at which you'd like to receive notifications from cPanel. This is an excellent idea for confirming that your tasks are being run.

Many users, however, might have a task that runs frequently, which can flood their inbox with notifications. If you want disable notifications on a specific cron task, add the following code to the end of your entry into the command line:

>/dev/null 2>&1

This will allow you to disable notifications on a per-command basis. On a related note, make sure that your cron commands don't run too frequently, as this can slow down the server and hinder data transfer to your visitors.

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclosure: We receive a compensation from some of the companies whose products are presented on our website.