Welcome Guest, Not a member yet? Register   Sign In
PHP won't run model functions from terminal
#1

[eluser]bndk[/eluser]
Good evening,

When I try to to run a controller's method from the terminal like so
Quote:php index.php some_controller some_function
with the following function body
Code:
function some_function() {
       echo 'I like eggs';
    }
it works like a charm, outputing the string as expected.

However, when I load a model and try to call one of its functions from the controller, nothing happens.
I tried to call the same model function from a web browser and it did what it was supposed to do, so I know there's not a problem with the function.

Does anyone have an idea as to why it doesn't work?

Your help is much appreciated Smile
#2

[eluser]CroNiX[/eluser]
My guess is that your app is relying on some $_SERVER variables, like $_SERVER['HTTP_HOST'] or similar, in order to do things like determine the database to use, etc. A lot of people use that to set their environment variables.

Problems can happen when accessing from the CLI because those variables aren't present (since the request isn't coming from a browser) and so it can't operate properly.

It's the main reason I've always used this method, as it allows you to set those variables as part of the CL request, or when used in a cron job. It's just more flexible.
#3

[eluser]bndk[/eluser]
I tried to use the method you described above. I set the CRON_CI_INDEX to my app's main index.php.
When I try to execute the controller's function I get a 404 error in return.
Is there something more I need to set up?
#4

[eluser]CroNiX[/eluser]
Here's what I have for my cron.php, which is in the same location as CI's index.php (with hardened permissions). Note the shebang with full path to php before php opening. I've disabled the logging in mine as I do the logging from my CI cron mothods, so it's commented out.
Code:
#!/usr/bin/php5
<?php
//Note: --server: is raw server name without http://

define('CRON_CI_INDEX', dirname(__FILE__) . '/index.php');
define('CRON', TRUE);   // Test for this in your controllers if you only want them accessible via cron

# Parse the command line
    $script = array_shift($argv);
    $cmdline = implode(' ', $argv);
    $usage = "Usage: cron.php --run=/controller/method [--show-output][-S] [--log-file=logfile] [--time-limit=N] [--server=http_server_name]\n\n";
    $required = array('--run' => FALSE);
    foreach($argv as $arg)
    {
        list($param, $value) = explode('=', $arg);
        switch($param)
        {
            case '--run':
                // Simulate an HTTP request
                $_SERVER['PATH_INFO'] = $value;
                $_SERVER['REQUEST_URI'] = $value;
                $required['--run'] = TRUE;
                break;

            case '-S':
            case '--show-output':
                define('CRON_FLUSH_BUFFERS', TRUE);
                break;

            case '--log-file':
                if(is_writable($value)) define('CRON_LOG', $value);
                else die("Logfile $value does not exist or is not writable!\n\n");
                break;

            case '--time-limit':
                define('CRON_TIME_LIMIT', $value);
                break;

            case '--server':
                $_SERVER['SERVER_NAME'] = $value;
                $_SERVER['HTTP_HOST'] = $value;
                break;

            default:
                die($usage);
        }
    }

    
    if(!defined('CRON_LOG')) define('CRON_LOG', 'cron.log');
    if(!defined('CRON_TIME_LIMIT')) define('CRON_TIME_LIMIT', 0);

    foreach($required as $arg => $present)
    {
        if(!$present) die($usage);
    }

# Set run time limit
    set_time_limit(CRON_TIME_LIMIT);

# Run CI and capture the output
    ob_start();

    chdir(dirname(CRON_CI_INDEX));
    require(CRON_CI_INDEX);           // Main CI index.php file
    $output = ob_get_contents();

    if(defined('CRON_FLUSH_BUFFERS')) {
        while(@ob_end_flush());        // display buffer contents
    } else {
        ob_end_clean();
    }

# Log the results of this run
//    error_log("### ".date('Y-m-d H:i:s')." cron.php $cmdline\n", 3, CRON_LOG);
//    error_log($output, 3, CRON_LOG);
//    error_log("\n### \n\n", 3, CRON_LOG);
Here's my actual cron job set to run at midnight (although it doesn't show that). Note the full path to php is used, the controller (in --run) starting with a slash, and how the server name is entered:
Code:
/usr/bin/php5 -q /home/site/public_html/cron.php --run=/cron/daily --server=www.site.com
#5

[eluser]bndk[/eluser]
Hmm...
This is strange. I've copied everything you did above, but I still keep getting an 404 error.
The whole page (html and css) are being output in the terminal.
I'm hosting the website locally by the way, so I guess the server name should be localhost, right?




Theme © iAndrew 2016 - Forum software by © MyBB