How to Automate Commands with Cron Jobs

Cron jobs allow you to automate certain commands or scripts on your site. You can set a command or script to run at a specific time every day, week, etc. For example, you could set a cron job to delete temporary files every week to free up disk space.

Notes: Exercise caution when you schedule cron jobs. If you schedule them to run too often, they may degrade performance.

Add a Cron Email

To open Cron Jobs, type Cron Jobs in the cPanel Search Tools and select Cron Jobs from the list.

The Cron Email section of the interface allows you to enter an email address for the system to send notifications when your cron jobs run. To set an email address, perform the following steps:

  1. In the Email text box, enter the email address at which you wish to receive the notifications.
  2. Click Update Email.
Cron Email

Disable email notifications

To disable email notifications for all cron jobs, remove the email address.

To disable email notifications for a single cron job, perform the following steps:

  1. Locate the cron job for which to disable email notifications in the Current Cron Jobs table and click Edit.
  2. In the Command text box, add the /dev/null 2>&1 line to the end of the command. For example: /usr/local/cpanel/bin/is_script_stuck /dev/null 2>&1
  3. Save your changes.

Add a Cron Job

To create a cron job, perform the following steps:

  1. Select the interval at which you wish to run the cron job from the appropriate menus, or enter the values in the text boxes.
    1. Common Settings — Select a commonly-used interval. The system will configure the appropriate settings in the MinuteHourDayMonth, and Weekday text boxes for you.
    2. Minute — The number of minutes between each time the cron job runs, or the minute of each hour on which you wish to run the cron job.
    3. Hour — The number of hours between each time the cron job runs, or the hour of each day on which you wish to run the cron job.
    4. Day — The number of days between each time the cron job runs, or the day of the month on which you wish to run the cron job.
    5. Month — The number of months between each time the cron job runs, or the month of the year in which you wish to run the cron job.
    6. Weekday — The days of the week on which you wish to run the cron job.
  2. In the Command text box, enter the command that you wish the system to run.
  3. Click Add New Cron Job.
Cron Jobs Common Settings

Important:

  • You must specify settings for the MinuteHourDayMonthWeekday, and Command text boxes.
  • Exercise extreme caution when you use the rm command in a cron job. If you do not declare the correct options, you may delete your home directory’s data.

Note:

Specify the absolute path to the command that you wish to run. For example, if you wish to run the public_html/index.php file in your home directory, enter the following command:

/home/user/public_html/index.php

View existing Cron Jobs

The Current Cron Jobs table displays your existing cron jobs.

Edit a Cron Job

To edit a cron job, perform the following steps:

  1. Locate the cron job that you wish to edit and click Edit.
  2. Edit the settings that you wish to change and click Edit Line.

Delete a Cron Job

To delete a cron job, perform the following steps:

  1. Click Delete next to the cron job that you wish to delete.
  2. Click Delete.

Thanks for visiting. For queries and suggestions, emails are welcome at learnweb@hostingcolumn.com.

Subscribe to Hosting Column for latest updates and posts.