KB: How to Create a Cron Job in cPanel
A cron job is a command that schedules scripts or commands to run automatically on your server at a specified time and date. This is useful for automating repetitive tasks like backups, sending newsletters, or cleaning up temporary files. Here is a guide on how to create a cron job in cPanel.
Step 1: Access the Cron Jobs Interface
Log in to your cPanel account.
In the Advanced section, click on the Cron Jobs icon.
Step 2: Configure the Cron Job Schedule
In the Add New Cron Job section, you will define the schedule for your task. You can use the "Common Settings" dropdown to choose a predefined schedule, or you can manually set the time using the fields for Minute, Hour, Day, Month, and Weekday.
For example, to run a script every day at 3:00 AM, you would set:
Step 3: Enter the Command
In the Command field, enter the command you want to execute. The command needs to specify the program to run and the path to your script. Below are two common examples.
Example 1: Executing a PHP Script
To run a PHP script directly on the server, use the PHP interpreter path followed by the script's path. This method is efficient as it doesn't involve a web server request. Replace /home/youruser/script.php with the actual path to your PHP file.
Example 2: Running a Script as a Browser Would
If your script requires a web server environment or is designed to be executed as if a browser were visiting it, you can use a command-line browser like lynx. This is useful for scripts that rely on a browser's context or a specific web URL.
Step 4: Add the Cron Job
After setting the schedule and entering the command, click the Add New Cron Job button. The cron job will now be active and will run according to your specified schedule.
You can view all your active cron jobs in the Current Cron Jobs section. From there, you can edit or delete them as needed.