Andy's Blog » CodeIgniter 命令行, codeigniter cron, cron job, request_uri, cronjob, path_info, codeigniter Cron job, codeigniter set_time_limit, codeigniter cli cron, codeigniter request_uri, codeigniter cron www, codeigniter 命令行 REMOTE_ADDR, codeigniter crontab, 命令行执行 CI框架 » CodeIgniter & Cron 命令行运行 CodeIgniter

CodeIgniter & Cron 命令行运行 CodeIgniter

方法1:直接修改 $_SERVER['PATH_INFO'] 和 $_SERVER['REQUEST_URI'] 以改变 controller

#!/usr/bin/php
<?php
/* make sure this isn't called from a web browser */
if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied.');
 
/* set the controller/method path */
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = '/cron/purge_cache';
 
/* raise or eliminate limits we would otherwise put on http requests */
set_time_limit(0);
ini_set('memory_limit', '256M');
 
/* call up the framework */
include(dirname(__FILE__).'/index.php');

方法2:用 $argv 获取参数,转换成controller

#!/usr/local/bin/php
<?php
 
/**
 * only a few lines of code will make the best web framework
 * function on the command line
 */

 
    
/* we don't need to be limited by...normal limitations */
    
set_time_limit(0);
    
ini_set('memory_limit', '256M');
 
    
/* make sure this isn't being called by a web browser */
    
if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied.');
 
    
/* set some constants */
    
define('CMD', 1);
 
    
/* manually set the URI path based on command line arguments... */
    
unset($argv[0]); /* ...but not the first one */
    
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = '/' . implode('/', $argv) . '/';

然后可以这样运行

./cli.php cron purge_cache

Incoming search terms:

Tags: cron, cron job, CodeIgniter, 命令行, SSH, argv, CLI, PATH_INFO, REQUEST_URI

本文地址: http://www.21andy.com/new/20100402/1797.html