How to setup a Cron job ?

Scheduling a real cron job

Log into your cPanel.

Navigate to the Advanced section.

Click Cron jobs.

The Cron Email section will send an email out after the Cron has finished.

Here is a simple cron job:

example-cron-job


There are two main parts:

  1. The first part is “0,30 * * * *”. This is where we schedule the timer.
  2. The rest of the line is the command as it would run from the command line.

The command itself in this example has three parts:

  1. “php”. PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
  2. “/home directory/public_html/folder-name/admin/cron.php”. This is just the path to the script.
  3. “> /dev/null 2>&1”. This part is handling the output of the script. More on bellow.

If you do not handle the output of the cron script, it will send them as e-mails to your user account on the server.

If you put “> /dev/null 2>&1” at the end of the cron job command (or any command), the output will be discarded.

This is the first part of the cron job string, as mentioned above. It determines how often and when the cron job is going to run.

It consists of five parts:

  1. minute
  2. hour
  3. day of month
  4. month
  5. day of week

Here is an screenshot for better understanding:

cron-job-timer

Common Settings include every minute to once a year.

Enter in the desired Command and click Add New Cron Job button.

Command lines. Examples only! Bold sections must be changed.

Note:You may need to adjust /home/ to /home#/ depending on the home directory your account resides on. 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 PHP5 cron job:

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 shell script cron job:

/bin/sh /home/username/public_html/file.sh

MySQL

Note: It is 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 is way your password stays secure and is never 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

Custom Installations without Easy Apache 3 (VPS/Dedicated)

PHP

Command to run for a PHP5 cron job:

/usr/local/php5/bin/php5 /home/username/public_html/cron.php

Command to run for a PHP4 cron job:

/usr/bin/php /home/username/public_html/cron.php

Cron job limitations and Solution is here:

you can use ini_set to bump the memory_limit and max_execution_time (ususally infinite in command though) – that will be help you to do but I think EasyCron will be good for you so you can choose the service from here to overcome all complexity.
Enjoy your business with EasyCron without hassle 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *


*