CentOS minimal template’s issue with crontab

On my CentOS 5.x 64bit XEN PV template (XEN PV is a virtualization type, this means I am talking about a VPS) doing ‘crontab -e’ gave me the following error

[root@vpsadmin ~]# crontab -e
no crontab for root - using an empty one
/bin/sh: /bin/vi: No such file or directory
crontab: "/bin/vi" exited with status 127

To correct this issue, do the following

[root@vpsadmin ~]# touch ~/.bashrc
[root@vpsadmin ~]# export VISUAL=nano
[root@vpsadmin ~]# source ~/.bashrc

So, now if you do ‘crontab -e’ it will work as it should

[root@vpsadmin ~]# crontab -e
no crontab for root - using an empty one
crontab: no changes made to crontab

Creating and Installing crontabs using CodeIgniter

Last night, I was trying to get a crontab working in CodeIgniter, since my current project (a personal project) is using CodeIgniter now. I didn’t wanted to use the conventional approach of running crontabs and my code seperate.

Unfortunately, CodeIgniter does not have support for Crontabs and I didn’t wanted to user wget in linux crontab to initiate unwanted requests.

I spent some time and came up with a solution. It uses

  1. CodeIgniter backend
  2. Does not modify any core architecture of CodeIgniter
  3. Keeps configuration at one place

So enough of the summary and let me show you how its done.
Create a file (e.g. cron.php) in the same place as your index.php and system folder. Here is its code

/**
* @author 	    Asim Zeeshan
* @web         http://www.asim.pk/
* @date		13th May, 2009
* @copyright 	No Copyrights, but please link back in any way
*/

/*
|---------------------------------------------------------------
| CASTING argc AND argv INTO LOCAL VARIABLES
|---------------------------------------------------------------
|
*/
$argc = $_SERVER['argc'];
$argv = $_SERVER['argv'];

// INTERPRETTING INPUT
if ($argc > 1 && isset($argv[1])) {
$_SERVER['PATH_INFO'] 	= $argv[1];
$_SERVER['REQUEST_URI'] = $argv[1];
} else {
$_SERVER['PATH_INFO'] 	= '/crons/index';
$_SERVER['REQUEST_URI'] = '/crons/index';
}

/*
|---------------------------------------------------------------
| PHP SCRIPT EXECUTION TIME ('0' means Unlimited)
|---------------------------------------------------------------
|
*/
set_time_limit(0);

require_once('index.php');

/* End of file test.php */

Now, we need a controller e.g. test so the controller code could be something like this. Please note that normally we do not need to output anything from these controllers since they are doing some background work or sending emails but for the sake of giving an example, I will be output-ing something to elaborate the example.

/**
* @author 	    Asim Zeeshan
* @web         http://www.asim.pk/
* @date		13th May, 2009
* @copyright 	No Copyrights, but please link back in any way
*/</code>

class Test extends Controller {

function __construct()
{
parent::Controller();
}

function index()
{
echo "testing from index \n";
}

function test() {
echo "testing from test \n";
}
}

Now execute crontab on linux command prompt

php /full-path-to-cron-file/cron.php /test/index

Voila! all you need now, is to setup the crontab like you normally do.
This code is not shared under any license so feel free to copy/modify/use it. Please link back to this website / post in any way e.g. direct link, credits etc.

P.S. The examples above user PHP5 constructor, If you need to execute this code on PHP4, please modify the constructors.