如何通过CRON运行CodeIgniter文件?

时间:2020-12-17 23:55:59

I've tried the following method in the past:

我过去尝试过以下方法:

<?php
set_time_limit(0);
$_SERVER['PATH_INFO'] = 'cron/controller/index';
$_SERVER['REQUEST_URI'] = 'cron/controller/index';
require_once('index.php');
?>

and putting this in a file in the codeigniter installation directory, calling it cron.php, and then invoking it via:

并将其放在codeigniter安装目录中的一个文件中,将其命名为cron.php,然后通过以下方式调用它:

php /home/[username]/public_html/my_project/cron.php

If I type the URL to cron.php in my browser it works perfectly, however whenever its run via CRON I get a 404 error. Putting the following code in the show_404() function of CodeIgniter

如果我在浏览器中输入cron.php的URL,它可以很好地工作,但是每当它通过CRON运行时我都会收到404错误。将以下代码放在CodeIgniter的show_404()函数中

function show_404($page = '')
{
   print_r($_SERVER);
   echo "\n\n";
   die ($page);
}

results in getting the following output emailed to me:

导致以下输出通过电子邮件发送给我:

Array
(
   [SHELL] => /bin/sh
   [MAILTO] => me@gmail.com
   [USER] => [me]
   [PATH] => /usr/bin:/bin
   [PWD] => /home/[me]
   [SHLVL] => 1
   [HOME] => /home/[me]
   [LOGNAME] => [me]
   [_] => /usr/bin/php
   [PHP_SELF] =>
   [REQUEST_TIME] => 1266479641
   [argv] => Array
       (
           [0] => /home/[me]/public_html/my_project/cron.php
       )

   [argc] => 1
   [PATH_INFO] => cron/controller/index
   [REQUEST_URI] => cron/controllers/index
)


home/[me]

Here I've [me] in place of my actual username.

在这里,我[我]代替我的实际用户名。

Any ideas?

有任何想法吗?

7 个解决方案

#1


21  

The simplest way to run a cron via CodeIgniter is to make a cron URL available via your app.

通过CodeIgniter运行cron的最简单方法是通过您的应用程序提供cron URL。

Then call it via wget

然后通过wget调用它

wget -O - -q -t 1 http://www.example.com/cron/run

Inside the controller you can then use a log to ensure the cron is not run too often i.e. if the Google robots trigger it by mistake.

在控制器内部,您可以使用日志来确保cron不会经常运行,即如果Google机器人错误地触发它。

A second method would be to use lynx

第二种方法是使用lynx

/usr/local/bin/lynx -source http://www.example.com/cron/run

#2


10  

Codeigniter has published documentation on how to do this: http://ellislab.com/codeigniter/user-guide/general/cli.html

Codeigniter发布了有关如何执行此操作的文档:http://ellislab.com/codeigniter/user-guide/general/cli.html

#3


5  

You may also like to add --spider to ignore the response. This stops the request from timing out:

您可能还想添加--spider来忽略响应。这会停止请求超时:

wget -O - -q -t 1 --spider http://www.example.com/cron/run

#4


4  

You might also want to check this out: Cron job bootstrapper

您可能还想检查一下:Cron作业引导程序

This is a simple bootstrapper file that you can use to directly run your CodeIgniter controllers from the commandline. It’s a very easy and elegant solution for using CI controllers for cron jobs. It also supports logging.

这是一个简单的引导程序文件,您可以使用它从命令行直接运行CodeIgniter控制器。对于使用CI控制器进行cron作业,这是一个非常简单和优雅的解决方案。它还支持日志记录。

#5


2  

There is a wiki article about how to run CodeIgniter on the command line, but this is more useful for applications that need to interact with the user through terminal (there's a library for that too).

有一篇关于如何在命令行上运行CodeIgniter的wiki文章,但这对于需要通过终端与用户交互的应用程序更有用(也有一个库)。

http://codeigniter.com/wiki/CI_on_the_command_line/

http://codeigniter.com/wiki/CI_on_the_command_line/

One benefit of doing it this way over using wget is you can protect your code from being run by users or bots with:

使用wget这样做的一个好处是,您可以保护您的代码不被用户或机器人运行:

if(!empty($_SERVER['HTTP_HOST']))
{
     show_error('Shove off hax0r!');
}

#6


2  

If you want to run cron job by running url, here is a great article

如果你想通过运行url来运行cron job,这里有一篇很棒的文章

http://www.nbill.co.uk/documentation/setting-up-a-cronjob.html

http://www.nbill.co.uk/documentation/setting-up-a-cronjob.html

#7


0  

Use php-cli instead of php
Ex:

使用php-cli而不是php Ex:

/usr/bin/php-cli  /home/CPANEL_USER/public_html/index.php cronJobs deleteNotifications 

#1


21  

The simplest way to run a cron via CodeIgniter is to make a cron URL available via your app.

通过CodeIgniter运行cron的最简单方法是通过您的应用程序提供cron URL。

Then call it via wget

然后通过wget调用它

wget -O - -q -t 1 http://www.example.com/cron/run

Inside the controller you can then use a log to ensure the cron is not run too often i.e. if the Google robots trigger it by mistake.

在控制器内部,您可以使用日志来确保cron不会经常运行,即如果Google机器人错误地触发它。

A second method would be to use lynx

第二种方法是使用lynx

/usr/local/bin/lynx -source http://www.example.com/cron/run

#2


10  

Codeigniter has published documentation on how to do this: http://ellislab.com/codeigniter/user-guide/general/cli.html

Codeigniter发布了有关如何执行此操作的文档:http://ellislab.com/codeigniter/user-guide/general/cli.html

#3


5  

You may also like to add --spider to ignore the response. This stops the request from timing out:

您可能还想添加--spider来忽略响应。这会停止请求超时:

wget -O - -q -t 1 --spider http://www.example.com/cron/run

#4


4  

You might also want to check this out: Cron job bootstrapper

您可能还想检查一下:Cron作业引导程序

This is a simple bootstrapper file that you can use to directly run your CodeIgniter controllers from the commandline. It’s a very easy and elegant solution for using CI controllers for cron jobs. It also supports logging.

这是一个简单的引导程序文件,您可以使用它从命令行直接运行CodeIgniter控制器。对于使用CI控制器进行cron作业,这是一个非常简单和优雅的解决方案。它还支持日志记录。

#5


2  

There is a wiki article about how to run CodeIgniter on the command line, but this is more useful for applications that need to interact with the user through terminal (there's a library for that too).

有一篇关于如何在命令行上运行CodeIgniter的wiki文章,但这对于需要通过终端与用户交互的应用程序更有用(也有一个库)。

http://codeigniter.com/wiki/CI_on_the_command_line/

http://codeigniter.com/wiki/CI_on_the_command_line/

One benefit of doing it this way over using wget is you can protect your code from being run by users or bots with:

使用wget这样做的一个好处是,您可以保护您的代码不被用户或机器人运行:

if(!empty($_SERVER['HTTP_HOST']))
{
     show_error('Shove off hax0r!');
}

#6


2  

If you want to run cron job by running url, here is a great article

如果你想通过运行url来运行cron job,这里有一篇很棒的文章

http://www.nbill.co.uk/documentation/setting-up-a-cronjob.html

http://www.nbill.co.uk/documentation/setting-up-a-cronjob.html

#7


0  

Use php-cli instead of php
Ex:

使用php-cli而不是php Ex:

/usr/bin/php-cli  /home/CPANEL_USER/public_html/index.php cronJobs deleteNotifications