Welcome Guest, Not a member yet? Register   Sign In
Super Simplified CRON
#1

[eluser]BradEstey[/eluser]
I've tried two different methods of getting CRON to run CodeIgniter scripts today. They're the top two results on Google when you search "CRON Codeigniter"

http://codeigniter.com/wiki/Category:Adv...ronScript/
http://jonathonhill.net/codeigniter/cron/

After getting a little frustrated with none of these solutions working on my server (don't ask me why Confusedhut: ) I came up with my own super simplified solution..

1. Create your controller like normal. Mine is just simply cron.php, it contains a script that deletes all of the files in an ftp directory:

Code:
<?php

class Cron extends Controller {

    function Cron()
    {
        parent::Controller();    
        $this->load->model('Cron_model');
        if ($this->Cron_model->isCron($_SERVER['REMOTE_ADDR']) == false) {
         redirect('404', 'location'); }
    }

    function deleteftpfiles()
    {        
        $this->load->library('ftp'); // Load FTP Library.
        
        $config['hostname'] = '192.168.1.1';
        $config['username'] = 'username';
        $config['password'] = 'password';
        $config['debug'] = False;
        
        $this->ftp->connect($config); // Open FTP Connection.
        
        // Empty the httpdocs directory.
        $list = $this->ftp->list_files('/httpdocs/');
        foreach($list as $file) { $this->ftp->delete_file($file); }
        
        $this->ftp->close(); // Close FTP Connection
    }    
}


2. Create cron_model.php with the isCron() funtion. This function just checks to make sure the IP address is the IP of your server, to make sure that your cron script can't be accessed directly by users. If you have an array of possible Server IPs, then you would check them here.. if not then you might not need this model at all.

Code:
<?php

class Cron_model extends Model {

    function Cron_model()
    {
        parent::Model();
    }

    function isCron($ip) { if ($ip == '192.168.1.1') { return true; } else { return false; } }

}

3. Create a file somewhere on your server called deleteftpfiles.php. I put it in a private folder below my root directory so that it can't be accessed directly. This file only needs one line of code:

Code:
<?php file_get_contents('http://www.yourdomain.com/cron/deleteftpfiles'); ?>

4. And finally, in crontab, I've opted to run it every Sunday at 12:00 AM:
Code:
0 0    *    *    sun    /usr/bin/php -q /var/www/vhosts/yourdomain/private/deleteftpfiles.php



This probably isn't the most elegant solution.. but it works for me!
#2

[eluser]loosetops[/eluser]
Check out this post

http://ellislab.com/forums/viewthread/15...15/#742676




Theme © iAndrew 2016 - Forum software by © MyBB