ISPA Winner 2018 16 Years of Krystal

Web Hosting

Please find below our list of Web Hosting guides to help with your hosting account.

How to configure a cron job

This article was posted in: Web Hosting

Cron is a task scheduler that is built into the linux operating system. It is very flexible, and allows you to schedule all kind of automated tasks. The cPanel Cron jobs icon in the Advanced section of cPanel provides a simple graphical interface to using this great feature.

Here are some guidlines to get you started.

Running a PHP script

Lets say your cpanel account username is krystald and your file is in public_html/somedir/task.php - then you would enter :

/usr/bin/php -q /home/krystald/public_html/somedir/task.php

This tells PHP to run your script quietly [-q] so that it doesn't echo anything to the cron scheduler. You have to put the full path to your script. All cPanel account home directories are at /home/[username]/ as above.

Running a wget command (great for firing off WordPress's wp-cron.php)

WordPress' wp-cron.php script can be activated by fetching the URL wp-cron.php?doing_wp_cron as follows:

/usr/bin/wget -O /dev/null http://www.mydomain/wp-cron.php?doing_wp_cron

Here, the [-O /dev/null] switch tells wget to send any output to /dev/null, which is linux-speak for saying send any output to the linux bit bucket, never to be seen again.

Running an executable script directly

Running a script as an executable is also possible, provided the script contains the correct shebang. In this case you can simply enter the full path to your script. For example:

/home/krystald/ > /dev/null 2>&1

A few things to note here are :

The file must have the correct shebang entry at the top - on our servers,they are as follows:

  • #!/usr/local/bin/php
  • #!/usr/local/bin/perl
  • #!/usr/bin/python
  • #!/bin/sh
  • #!/bin/bash

The file should have the correct permissions - usually chmod 755 - however, if the file contains sensitive data and is outside of your public_html folder, then you should ensure it resides within a folder set to the nobody group. We can do this for you, but it's much more secure so let us know if you need this help.

> /dev/null 2>&1

What's this for, you ask? Let's break it down...

  • [>] this is the linux redirection symbol - it basically says "send any messages that come out of the process on my left to the target on my right".
  • [/dev/null] as we have already covered is the linux black-hole - the instant trash can.
  • [2>&1] The number 2 represents the error stream, so if your application produces an error, then these messages will squirt out of this number 2 - charming! The following [>] redirection symbol then sends any errors generated to [&1] which is a fancy way of saying repeat the first argument, or in other words [/dev/null] again.

So, basically, > /dev/null 2>&1 is a way of sending all output the script might generate to the bit bucket.