This documentation is valid for cPanel & WHM versions 82 through the latest version, with the content last modified on May 6, 2024.

Overview of Cron Jobs

Cron jobs are an essential feature for automating various tasks on your web server. They allow you to schedule commands or scripts to run at specific times or regular intervals. Typically, a cron job executes a series of simple tasks defined within a script file, making it an invaluable tool for system maintenance, backups, and other recurring operations.

Important Considerations:

  • Exercise considerable caution when scheduling cron jobs. It is highly recommended to allocate sufficient time between cron job executions to ensure that the previous task completes before the next one begins. Overlapping cron jobs, where a new job starts before its predecessor finishes, can lead to performance degradation and unexpected issues on your server.
  • For a deeper understanding of cron jobs and their advanced configurations, we recommend consulting Red Hat’s comprehensive documentation on Scheduling a Cron Job.

Configuring Cron Job Email Notifications

The Cron Email section within the cPanel interface provides a convenient way to receive notifications whenever your scheduled cron jobs execute. To ensure you are informed about the status and output of your automated tasks, follow these simple steps to set up an email address for notifications:

  1. In the Email text box, enter the specific email address where you wish to receive these cron job notifications.
  2. Click Update Email to save your changes.

Disabling Email Notifications

There may be instances where you prefer not to receive email notifications for certain or all cron jobs. Here's how to manage these settings:

Disabling Notifications for All Cron Jobs

To completely disable email notifications for every cron job configured on your account, simply remove the email address from the Email text box in the Cron Email section and click Update Email.

Disabling Notifications for a Single Cron Job

If you only want to disable email notifications for a particular cron job, follow these steps:

  1. Locate the specific cron job in the Current Cron Jobs table for which you want to disable email notifications, and then click the Edit button next to it.
  2. In the Command text box, append the string /dev/null 2>&1 to the very end of the existing command. This redirects all output (both standard output and errors) to a null device, preventing any email from being sent. For example:
    /usr/local/cpanel/bin/is_script_stuck /dev/null 2>&1
  3. Click Edit Line to save your modifications.

Creating New Cron Jobs

To add a new cron job and automate a command or script on your server, perform the following steps:

  1. Select the desired interval for your cron job's execution using the provided menus, or manually enter the specific values in the corresponding text boxes:
    • Common Settings: This menu offers a selection of frequently used intervals (e.g., "Once Per Hour," "Once Per Day"). Choosing one of these options will automatically populate the appropriate settings in the Minute, Hour, Day, Month, and Weekday text boxes for you.
    • Minute: Specify the minute of each hour (0-59) or the number of minutes between each execution.
    • Hour: Define the hour of each day (0-23) or the number of hours between each execution.
    • Day: Set the day of the month (1-31) or the number of days between executions.
    • Month: Choose the month of the year (1-12) or the number of months between executions.
    • Weekday: Select the specific days of the week (0-7, where 0 and 7 represent Sunday) on which the cron job should run.
  2. In the Command text box, carefully enter the command or the path to the script that you wish the system to execute.

Important Notes for Commands:

  • You must specify settings for all fields: Minute, Hour, Day, Month, Weekday, and the Command text box. All fields are mandatory for a valid cron job.
  • Exercise extreme caution when utilizing the rm (remove) command within a cron job. An incorrect declaration of options or path can lead to the unintended and irreversible deletion of critical data, potentially including your entire home directory.
  • If your cron job is designed to run a custom script, that script must have the necessary execute permissions set. For detailed information on managing file permissions in Linux, refer to Red Hat’s Linux Permissions documentation.

Path Specification:

Always specify the absolute path to the command or script you intend to run. For example, if you wish to execute the index.php file located within your public_html directory, the correct command would be:

/home/user/public_html/index.php

Replace user with your actual cPanel username.

  1. After configuring all settings, click Add New Cron Job to create and schedule your task.

Cron Job Example: Running a Script Hourly

Let's consider a practical example: running a script that resides in your home directory once every hour. Follow these steps to set it up:

  1. In the Common Settings menu, select the Once Per Hour (0 * * * *) option. This will automatically configure the minute field to 0 and the other time fields to *, indicating every hour.
  2. In the Command text box, enter the absolute path to your script. For instance, if your script is named myscript.sh and is located in your home directory, you might enter:
    /home/user/myscript.sh

    Remember: You must ensure that the script file has the correct file permissions set to allow it to execute. Typically, this involves setting execute permissions (e.g., chmod +x myscript.sh).

  3. Click Add New Cron Job. The cPanel interface will then display a success message confirming the creation of your cron job.

Managing Existing Cron Jobs

The Current Cron Jobs table provides a comprehensive overview of all cron jobs currently configured under your cPanel account. From this table, you can easily review, modify, or delete any scheduled task.

Editing a Cron Job

Should you need to adjust the timing or command of an existing cron job, follow these steps:

  1. Locate the specific cron job that you wish to modify in the Current Cron Jobs table, and then click the Edit button associated with it.
  2. Make the necessary changes to the settings (e.g., time intervals, command) in the editable fields.
  3. Click Edit Line to save your updated cron job configuration.

Deleting a Cron Job

If a scheduled task is no longer required, you can easily remove it from your system:

  1. Click the Delete button located next to the cron job that you wish to remove from the Current Cron Jobs table.
  2. A confirmation prompt will appear. Click Delete again to confirm and permanently remove the cron job.

Further Resources

For additional documentation and related topics within cPanel & WHM, explore the following resources:

Was this answer helpful? 0 Users Found This Useful (0 Votes)