Codeigniter cron job not working

谁都会走 提交于 2019-11-26 23:13:20

the steps to prepare CodeIgniter 2.x for cron-jobs via CLI (command line interface):

1st: create a copy of your root index.php file and save it in your root as cli.php

2nd: in your cli.php replace <?php with this code:

#!/usr/local/bin/php
<?php

/* override normal limitations */
set_time_limit(0);
ini_set('memory_limit', '256M');

/* deny direct call from web browser */
if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied.');

/* constants */
define('CMD', 1);

/* manually set the URI path based on command line arguments... */
unset($argv[0]); /* except the first one */
$_SERVER['QUERY_STRING'] =  $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = '/' . implode('/', $argv) . '/';

3rd: execute your cron job like this:

/usr/bin/php /var/www/website/public_html/cli.php controller method

where /var/www/website/public_html/ is your server's home directory, the location of your index.php and cli.php.

notes:

for CI 3.0 you find the necessary information here

database: you'll need to provide your db config settings in your controller method, as the cron job just executes the controller's method. So it doesn't know anything about any database settings!

$config['hostname'] = "localhost";
$config['username'] = "username_admin";
$config['password'] = "password";
//etc..

$this->db  = $this->load->database($config, TRUE);

debug: just add a link in your html to run your controller's method like: index.php/controller/method (remove that once you website is live)

source: very helpful

I think this is the simplest way to get codeigniter working under cron job,

You can use curl in cron jobs to execute or run the codeigniter in cron jobs Example:

* * * * * /usr/bin/curl https://www.domain.com/controller/function

you can also run cron file from terminal without cli.php.it is not compulsory file.

i am runnig cron from my server using following syntax: /usr/bin/php /var/www/project1/index.php controllerName methodName >> /var/log/project1/logFile.txt

It works fine for me.... -O /dev/null-q command line is used to remove the log file in server...

wget -O /dev/null -q http://www.mani.com/poject-name/index.php/tools/sendPushNotification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!