fb-pixel
Logo

How to create a cron job

December 22, 2021 / Published in:  from Ivan Messina
No comments

Forgetting to perform certain tasks is really frustrating for website managers. With a Cron Job, you can set up scheduled tasks for your server.

In this tutorial, I’ll explain what a Cron Job is and how it can help you streamline your work.

What is a Cron Job

A Cron Job is simply a scheduled action that the server performs on a regular basis.

Performing the same task over and over again can sometimes be quite a chore and tedious. Scheduling an action allows users to automate tasks on hosting.

Be careful when using this tool because inappropriate use may adversely affect your server’s performance.

How a Cron Job can be useful

Performing scheduled actions is useful to avoid performing tasks manually, saving time and avoiding tedious and repetitive tasks.

Listed below are just a few of the possible scenarios for using a Cron Job.

  • Antivirus system scan
  • User Account Management
  • Sending scheduled emails
  • Maintenance tasks, such as creating automated backups, checking database integrity, or cleaning the disk of temporary files.

Create a new Cron Job with cPanel

In order to create a new Cron Job you will first need to login to cPanel.

In the main cPanel dashboard, scroll down to the “Advanced” section and select “Cron Jobs“.

Cpanel Cron Jobs

In the first section of the page, you can manually set up an email to which you can redirect the result each time a Cron command is executed.

If you decide to enter an email, remember to hit the “Update Email” button to save the changes.

Cron Email Update Email

In the second section of the page, you can enter and configure a new Cron job.

Configure New Cron Job

Selecting the “Common Settings” field will open a drop-down menu from which you can choose from the various default settings.

You will see a number of options regarding script execution frequency, choose when you want the program to execute your Cron.

Cron Job Common Settings

Alternatively, you can create your own custom option by manually choosing individual fields involving the date and time.

In the last field “Command:” you must enter the command to execute. You can choose to execute a script present on your web space, indicating the path to its destination by following this syntax:

php -q /home/ACCOUNTNAME/public_html/script_to_execute.php

Once the configuration is finished, to save the changes you need to click the “Add New Cron Job” button.

Add New Cron Job

Delete a Cron job

If you want to delete a Cron Job, you can do so by simply locating the command to be deleted from the list and clicking on the “Delete” button.

Delete Cron Job

A confirmation message will appear, select “Delete” again to confirm the deletion.

Disable Cron Jobs email notifications

In the event that you no longer wish to receive email notifications for all Cron Jobs, you will need to remove your email from the appropriate field on cPanel.

Leave the email field blank and hit “Update Email“.

Cron Email Remove Email

If, on the other hand, you plan to disable the receipt of emails for certain Cron Jobs only, access the “Cron Job” section on cPanel and identify which one you wish to edit.

Hit the “Edit” button and at the end of your script, add the following line to completely disable receiving emails.

/dev/null 2>&1

Remember to save your changes by clicking the “Edit line” button.

Edit Cron Job

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.