How to setup a CRON job

Cron is a task scheduler built into the Linux operating system, allowing you to schedule automated tasks. cPanel provides a graphical interface to set up cron jobs easily.

Follow these steps to set up a CRON job:

  1. Log in to cPanel:
  • Access your cPanel account.
  1. Navigate to Cron Jobs:
  • In cPanel, go to Advanced > Cron Jobs.

 

 

  1. Configure Email Notifications (Optional):
  • You can specify an email address to receive notifications about your cron jobs.
  • Enter the desired email address in the top section and click Update Email.

 

 

  1. Add a Cron Job:
  • Scroll down to the “Add New Cron Job” section.

 

 

  • Choose the desired frequency for your cron job from the “Common Settings” dropdown or specify custom settings for Minute, Hour, Day, Month, and Weekday.
  • In the “Command” field, enter the command required to run the task. The format is usually <executable> <switches> <task command file>. For example:
/usr/bin/php -q /home/username/public_html/folder/task.php
  • /usr/bin/php is the path to the PHP executable.
  • -q is the ‘quiet’ switch.
  • /home/username/public_html/folder/task.php is the full path to the task command file.

 

  1. Editing or Deleting a Cron Job:
  • Active cron jobs will be listed in the “Current Cron Jobs” section at the bottom of the page.
  • You can edit or delete cron tasks here.
  • Click Edit to modify the time and task details, then click Edit Line to save your changes.
  • Click Delete to remove a cron task.

 

Additional Examples:

Running a wget command (for WordPress’s wp-cron.php):

Use wget to trigger WordPress’s wp-cron.php:

/usr/bin/wget -O /dev/null https://www.yourdomain.co.uk/wp-cron.php?doing_wp_cron
  • -O /dev/null sends any output to the Linux trash can.
  • Replace www.yourdomain.co.uk with your actual domain.

Running an executable script directly:

If the script has the correct shebang (e.g., #!/usr/local/bin/php) and permissions (usually chmod 755), you can run it directly:

/home/username/myscript.pl > /dev/null 2>&1
  • > /dev/null 2>&1 redirects all output to the Linux trash can.

 

Ensure your scripts have the necessary shebang and permissions for successful execution. The above examples provide flexibility for various types of cron jobs.