简体   繁体   English

Codeigniter Cron工作不起作用

[英]Codeigniter cron job not working

I have a cron job like this: 我有这样的计划工作:

/usr/bin/php /var/www/website/public_html/index.php className methodName

If I run it in terminal it runs, but outputs nothing. 如果我在终端中运行它,它将运行,但是什么也不输出。 If I pass a wrong method name it runs successfully. 如果我传递了错误的方法名称,它将成功运行。 If I pass wrong class name it outputs a website 404 error. 如果我传递了错误的类名,则会输出网站404错误。

I also have a routing which adds "en" into url, for example 我也有一个将“ en”添加到URL中的路由,例如

http://www.website.com/en/home/index

could this be the problem? 这可能是问题吗?

my settings of config.php are: 我的config.php设置是:

$config['uri_protocol'] = 'AUTO';
$config['index_page'] = '';

the steps to prepare CodeIgniter 2.x for cron-jobs via CLI (command line interface): 通过CLI (命令行界面)为cron-jobs准备CodeIgniter 2.x的步骤:

1st: create a copy of your root index.php file and save it in your root as cli.php 1:创建根index.php文件的副本并将其另存为cli.php

2nd: in your cli.php replace <?php with this code: 第二:在您的cli.php中,将<?php替换为以下代码:

#!/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: 第三:像这样执行您的Cron工作:

/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 . 其中/var/www/website/public_html/是服务器的主目录,即index.phpcli.php的位置。

notes: 笔记:

for CI 3.0 you find the necessary information here 对于CI 3.0,您可以在此处找到必要的信息

database: you'll need to provide your db config settings in your controller method, as the cron job just executes the controller's method. 数据库:您需要在控制器方法中提供数据库配置设置,因为cron作业仅执行控制器的方法。 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) 调试:只需在html中添加一个链接即可运行控制器的方法,例如: index.php/controller/method (一旦网站上线,请删除该链接)

source: very helpful 资料来源:非常有帮助

I think this is the simplest way to get codeigniter working under cron job, 我认为这是让Codeigniter在Cron工作下工作的最简单方法,

You can use curl in cron jobs to execute or run the codeigniter in cron jobs Example: 您可以在cron作业中使用curl来执行或运行cron作业中的codeigniter示例:

* * * * * /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. 您也可以从没有cli.php终端运行cron文件。它不是cli.php文件。

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 我使用以下语法从服务器上运行了cron: /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... 它对我来说很好。...-O / dev / null-q命令行用于删除服务器中的日志文件...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM