简体   繁体   English

如何在不使用wget的情况下在codeigniter中运行cron作业?

[英]How to run a cron job in codeigniter without using wget?

I want to run a cron job without using wget in CodeIgniter. 我想在CodeIgniter中不使用wget来运行cron作业。 I am using it like this: 我正在这样使用它:

*/1 * * * * wget  http://assurance.com/controller/function

It works successfully, but I do not want to use wget. 它可以成功运行,但是我不想使用wget。

Is there any another way to run this CodeIgniter script? 还有其他方法可以运行此CodeIgniter脚本吗?

You can try and use something like this: 您可以尝试使用以下方法:

* * * * * /usr/bin/php  /pathToTheApp/controller/function

But of course the /usr/bin/php should be your path to the PHP binaries and pathToTheApp should be the absolute path to your CI application. 但是,当然/usr/bin/php应该是您的PHP二进制文件路径,而pathToTheApp应该是您的CI应用程序的绝对路径。

如果您在主机上具有本地Shell访问权限,则将其添加到您的crontab中。

I needed to do this exact thing recently and couldn't find a complete solution. 我最近需要做这件事,却找不到完整的解决方案。 So I'll try to provide one here. 因此,我将在此处提供一个。

I used "bash shell" because I wanted to make a command line (CLI) call to my controller into of an http (note: I'm using an ubuntu/linux server. 我之所以使用“ bash shell”,是因为我想对我的控制器进行命令行(CLI)调用,使其进入http(注意:我正在使用ubuntu / linux服务器。

There are three main parts: 主要包括三个部分:

The cron call 定时呼叫

The shell script Shell脚本

The controller function 控制器功能


Cron: 克龙:

in your server cli type this to access crontab: crontab -e 在服务器cli中,键入以下内容以访问crontab:crontab -e

then add your cron call: * * * * * bash /path/to/script/test.sh 然后添加您的cron调用: * * * * * bash /path/to/script/test.sh

(note: I created a folder in my site root called cron, so my path would be: (请注意:我在网站根目录中创建了一个名为cron的文件夹,因此我的路径为:

/var/www/website/cron/test.sh /var/www/website/cron/test.sh


Shell script: Shell脚本:

In that cron folder we made, create a file called " test.sh " 在我们创建的cron文件夹中,创建一个名为“ test.sh”的文件

In the file put: 在文件中放入:

#!/bin/bash
cd /path/to/site
/usr/bin/php index.php controller function

That's it 而已

cron sets up the timer to call the file shell calls the controller from the CLI cron设置计时器以调用文件外壳程序,从CLI调用控制器

you'll now be able to use $this->input->is_cli_request() in your function for added security. 现在,您可以在函数中使用$ this-> input-> is_cli_request()来提高安全性。

public function cronTest()
{
    if($this->input->is_cli_request())
    {
        //code goes here;
    }        
}

Hope this helps save you some time, this took me way longer than expected :) 希望这可以帮助您节省一些时间,这使我花费了比预期更长的时间:)

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

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