How to Set Up and Delete a Cron Job

A cron job allows you to run a certain command at times set by the job. For example, you could set a cron job to delete temporary files every week so that your disk space is not being used up by those files.

ou will also want to read:

How to Create a MySQL Database, a User and Delete Database in Cpanel
How to Create a SubDomain in Cpanel
How to Create a Website/Domain Redirect in cPanel
How to force your website url with www or non-www using htaccess
How to optimize your website to use less server resources



Cron Jobs Detailed Tutorial

To access the Cron Jobs Menu, click on the corresponding icon located in the Advanced section on the main screen of your cPanel interface.

Adding a cron job

Step 1: To access the Cron Jobs Menu, click on the corresponding icon located on the main screen of your cPanel interface.

Step 2 (optional): Enter the email address where the cron output will be sent to in the corresponding field.

Step 3: Enter the command you wish the cron job to run in the blank field next to the Command field.

Step 4: Select how often you wish the command to be run by selecting an option in each of the boxes or by selecting a predefined period from the Common Settings drop-down menu.

Step 5: Click on Add New Cron Job to save your changes and set up your cron job.

Important The command run by the cron job must be a valid command.

Also read:

How to Reset or Change to a New CPanel Password
How to Backup My Website in cPanel
How to Setup Google Apps MX, CNAME and SPF records
How to upload files to a website
Overview of Quickinstall


Deleting a cron job

When a cron job is no longer needed, you should delete it so the command will not continue to run.

Step 1: To access the Cron Jobs Menu, click on the corresponding icon located on the main screen of your cPanel interface.

Step 2: Click on the Delete button next to the cron job you wish to remove.


Is my scheduled cron job running properly?

You can check if your scheduled cron job is running properly and if not, take appropriate steps to fix whatever issues.

Todhost monitors closely all operations on the servers and makes sure that all user cron jobs are being run on time.

If you'd will want to check your cron jobs you should provide a valid email account when setting the Cron jobs in cPanel.

When you specify a valid email you will receive the output of the cron job that is executed. Thus you will be able to check it and make sure everything has been executed correctly.

Please bear in mind that you will receive an email for each of the executed cron jobs. This may flood your inbox in case your crons run too often. Recommended you allow cron jobs to run once daily except it is very expedient to allow shorter intervals.

Also read:

PHP Configuration in cPanel
Step by Step Approach to Password Protect a Directory in cPanel
The htaccess file and its uses



How to Set Up the Cron Job Command

For Linux Web Hosting;

The commands below are compatible with Linux hosting plans only. These plans include Shared, Reseller, VPS, and Linux Dedicated Servers.

Note: These commands will not work on Windows Hosting or VPS packages with Plesk.

The commands provided below are examples of typical script locations. Some sections within each command will need to be replaced with your cPanel username and your script name, You will need to check and replace with the proper names..
Note: You may need to adjust /home/ to /home#/ depending on the home directory on which your account resides. To view the home directory for your account simply view the stats column on the main cPanel page of your account, and look for the home directory.

PHP

Command to run a PHP 5.5 cron job:
/opt/php55/bin/php /home/username/public_html/cron.php

Command to run a PHP 5.4 cron job:
/opt/php54/bin/php /home/username/public_html/cron.php

Command to run a PHP 5.3 cron job:
/opt/php53/bin/php /home/username/public_html/cron.php

Command to run a PHP 5.2 cron job:
/opt/php52/bin/php /home/username/public_html/cron.php

Optional flags are sometimes required for a PHP cron job:
php -q /home/username/public_html/cron.php

Command to use a specific php.ini file:
php -c /home/username/public_html/php.ini /home/username/public_html/myscript.php

Command to GET a remote file:
/usr/bin/GET http://www.example.com/file.php
Perl

Command to run a CGI cron job:
perl /home/username/public_html/cgi-bin/file.pl
SSH

Command to run a code script cron job:
/bin/sh /home/username/public_html/file.sh

MySQL

Note: It is a good practice to not type your password out in the follow commands but to simply use the -p flag alone and have the system prompt you for the password. This way your password stays secure and never exists on the server as plain text.

Command to import a database:
mysql -u mysql_user -ppassword database_name < backup.sql

Command to export a database:
mysqldump -u mysql_user -ppassword database_name > backup.sql


Why Your Cron Job May Not Run

If your cron job is not running, here are the most likkely reasons you should look out for.

1.) Wrong environment

Your user account operates under a specific environment where you have a direct path to the programs and utilities you need. cron operates under a limited path and does not always have access to the same programs. Your PATH is one such environment variable that tells the shell where to look for executable programs. If you’re trying to use a program that isn’t in your path, the shell will return an error. In the case of cron, your job will simply not run.

Solution – There are two things you can do in this case. The first involves using absolute paths in your script. This ensures cron has a complete path to the program you’re running:

/usr/app/command -x test-your-script-sample

Alternatively, you can set your path at the top of your script like:

#!/bin/bash
PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

This will give cron direct access to your program.

2.) Your scripts aren’t owned by root

From the man page:


If you’re placing scripts inside one of the /folder/cron.* directories, the scripts must be owned by root. If root does not own the script then cron will simply skip over it entirely. Many users choose to place scripts inside of these folders to bypass the requirements of their own crontab. Make sure your scripts are owned by root so things go smoothly.

3.) Your script names aren’t formatted properly

From the man page:

Your script cannot contain any special characters when placed into one of the /folder/cron.* directories. Linux does not require a file extension and your script will run just fine without one.

4.) Your cron script is not executable

Your cron script must have executable permissions in order for cron to run it. Use the following command to make your script executable if it isn’t.

chmod +x /path/to/script


5.) Incorrect permissions

Permissions are a very common problem especially for commands placed in crontab. crontab runs under the permissions of the crontab user. If you don’t have proper permissions under your user, cron is not going to have the proper permissions either. Always run your script or command prior to scheduling them with cron. If your commands don’t run or you get access denied errors, you know you need to make adjustments. If your script or command absolutely requires root permissions, then create a crontab as the root user of your system.

6.) cron daemon not running

You will find that a cron won't run if the deamon itself is not running.

  • 48 Users Found This Useful
Was this answer helpful?

Related Articles

I have a full backup of account through cPanel. How do I restore it?

It is recommended that you maintain healthy backup of your website and possibly download the...

How To Create, Edit, and Delete a File in cPanel Using File Manager

This tutorial will be useful for you to understand how to create a file, edit a file or delete...

How to Create and Manage a MySQL Database

Create MySQL Database and User via cPanel The MySQL Database Wizard is another useful tool...

How do I create and remove an Addon Domain?

An add-on domain is a separate domain name that you add to your web hosting subscription with...

How Do I Create and Delete a Parked Domain

Parked domains are additional domains which you host on your account. They will display the same...

Powered by WHMCompleteSolution