How to setup a cron job

In this tutorial, we will show you a few useful examples on how to create a cron job for your script-based websites.

Scripts usually include a working environment, which is sophisticated enough to work as a multi-structural base for performing a wide variety of tasks.

Those tasks altogether are easily implemented and combined into forums, informational portals, blogs, billing management systems, etc.

Although most often the implementation of a given script is done in real time and the users operate with a web-driven GUI to navigate through what the given script might offer, there are still some ”assignable” tasks that only site administrators can schedule.

Those tasks are what we call cron jobs – they carry important site/script maintenance information and pass system commands to the Cron Daemon via Crontab. Cron jobs are scheduled to execute shell commands or scripts at a given time or date.

Below you will find a few examples, which represent a shell command execution cron job and a script (cgi / php) execution cron job.

Cron jobs have the following formatting that we will divide into groups:

Cron job time schedule – [ Minute – Hour – Day – Month – Weekday ]

Cron job shell command – [ARGUMENTS]

Cron job script command – [PATH OF PHP/PERL] [ARGUMENTS] [PATH OF PHP SCRIPT]

Here is an example of cron job time scheduling and the allowed cron job operators:

.—————- minute (0 – 59) 
| .————- hour (0 – 23)
| | .———- day of month (1 – 31)
| | | .——- month (1 – 12) OR jan,feb,mar,apr … 
| | | | .—- day of week (0 – 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat 
| | | | |
* * * * * command to be executed

* * * * * => Execute every minute
0 * * * * => Execute every Hour
0 0 * * * => Execute every midnight
0 0 0 * * => Execute every Month
0 0 0 0 * => Execute every Weekdays

You can create cron jobs from your Control Panel.

To do that – navigate to Advanced > Cron Jobs and click on the ‘Create a New CronJob‘ button.

There you can also see the cron jobs that have already been created.

To set up a cron job – you must specify the email address where the results will be sent to, the date and the time when the job should be executed and the command, which should be executed.

Here is an example of running shell command driven cron jobs and script execution cron jobs:

0 * * * * rm -f * /home/www/my-best-domain.com/temp/

This command will initiate the rm (remove/delete) utility for file/folder deletion and will forcibly delete all the files under /home/www/my-best-domain.com/temp/

Let us say that you want to mail newsletters, site updates, etc. to your clients on a daily basis.

Most script systems have already prebuilt scripts for this purpose and many more automated services.

The cron job would look like this:

0 0 0 0 * /usr/bin/php /home/www/my-best-domain.com/mailcron.php

You can learn more on the allowed file types when using Crontab in the following tutorial: Files that could be executed with crontab – allowed file extensions.

Leave a Reply

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