(CronJob)命令行直接调用CodeIgniter中/controller/action

貌似CI不直接支持类似cakephp中的shell,下面是第三方的实现方法。

开源代码:https://github.com/compwright/codeigniter-cli-bootstrap

以下代码在CI 2.2.0下测试可用。

#!/usr/bin/php
<?php

/*
|--------------------------------------------------------------
| CRON JOB BOOTSTRAPPER
|--------------------------------------------------------------
|
| This section is used to get a cron job going, using standard
| CodeIgniter controllers and functions.
|
| 1) Set the CRON_CI_INDEX constant to the location of your
|    CodeIgniter index.php file
| 2) Make this file executable (chmod a+x cron.php)
| 3) You can then use this file to call any controller function:
|    ./cron.php --run=/controller/method [--show-output] [--log-file=logfile] [--time-limit=N] 
|
| GOTCHA: Do not load any authentication or session libraries in
| controllers you want to run via cron. If you do, they probably
| won't run right.
|
*/

define('CRON_CI_INDEX', 'index.php');   // Your CodeIgniter main index.php file
//define('CRON_CI_INDEX', '/var/www/vhosts/myaccount/index.php');   // Your CodeIgniter main index.php file
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]\n\n";
$required = array('--run' => FALSE);
$uri = '';

foreach($argv as $arg)
{
    list($param, $value) = explode('=', $arg);
    
    switch($param)
    {
        case '--run':
            // Simulate an HTTP request
            $uri = $value;
            $_SERVER['PATH_INFO'] = $value;
            $_SERVER['REQUEST_URI'] = $value;
            $_SERVER['SERVER_NAME'] = 'localhost';
            $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;

        default:
            die($usage);
    }
}

if (!defined('CRON_FLUSH_BUFFERS'))
{
    define('CRON_FLUSH_BUFFERS', FALSE);
}

$_SERVER['argv'][1] = $uri;

for ($i=2; $i<$argc; $i++)
    $_SERVER['argv'][$i] = '';

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(CRON_FLUSH_BUFFERS === TRUE)
{
    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\r\n", 3, CRON_LOG);
error_log(str_replace("\n", "\r\n", $output), 3, CRON_LOG);
error_log("\r\n// \r\n\r\n", 3, CRON_LOG);

echo "\n\n";

当然也可以通过使用get_content($url)或者Curl等调用控制器中的相关方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值