繁体   English   中英

如何在服务器上为Codeigniter脚本设置Cron作业

[英]How to setup cron job for a codeigniter script on server

嗨,我正在尝试使用cpanel在服务器上为脚本设置cron作业,但是我从未这样做过,如果有人可以指导我,那将真的有帮助。

脚本路径:/home/name/public_html/application/modules/stockupdate/controllers/stockupdate.php

我已经阅读了教程,并且需要尝试使用以下命令,每个小时都要运行一次此脚本:

1) php /home/name/public_html/index.php stockupdate 
2)/home/name/public_html/application/modules/stockupdate/controllers/stockupdate.php

第一个发送以下错误:

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index: REMOTE_ADDR</p>
<p>Filename: core/Input.php</p>
<p>Line Number: 351</p>

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  Cannot modify header information - headers already sent by (output started at /home/name/public_html/system/core/Exceptions.php:185)</p>
<p>Filename: libraries/Session.php</p>
<p>Line Number: 689</p>

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  Cannot modify header information - headers already sent by (output started at /home/name/public_html/system/core/Exceptions.php:185)</p>
<p>Filename: helpers/url_helper.php</p>
<p>Line Number: 542</p>

第二个命令向我发送错误消息,拒绝访问。 如果有人可以指导我创建Cron工作,我将不胜感激。 谢谢

stockupdate.php

<?php
class StockUpdate extends Backend_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('ftp');
        $this->load->library('csvimport');
        $this->load->model('stock_update_model');
        ini_set('memory_limit', '3000M');
        set_time_limit(0); 
    }

    function index()
    {
        if ($this->input->is_cli_request())
        {
            $this->stock_update_model->truncate_stock();
            $this->update_stock();
        }
    }

    public function update_stock()
    {
        $ftpServer = "server";
        $ftpUser = "username";
        $ftpPassword = "password";

        $ftp_server = $ftpServer;
        $ftp_conn = ftp_connect($ftp_server);
        $ftp_login = ftp_login($ftp_conn, $ftpUser, $ftpPassword ); 

        if(!$ftp_conn) 
            die("A connection to $ftpServer couldn't be established"); 
        else if(!$ftp_login) 
            die("Your login credentials were rejected"); 
        else
        {
            $stock_file = "/home/name/files/uploads/stock.CSV";

            $server_file = "/stock.CSV"; 

            if (ftp_get($ftp_conn, $stock_file, $server_file, FTP_BINARY)) 
            {
                if (($handle = fopen($stock_file, "r")) !== FALSE) 
                {
                    while (($data = fgetcsv($handle)) !== FALSE) 
                    {
                        $product_code = $data[0];
                                $stock = $data[1];


                                $this->stock_update_model->update_stock($product_code, $stock); 
                                }
                                else
                                {
                                    continue;
                                }
                    }
                    fclose($handle);
                    unlink($stock_file);
                }
                else
                {
                    echo "Error reading stock file.\n";
                }
            }
            else 
            {
                echo "There was a problem\n";
            }
        }
    }

我用这个表扬

/usr/bin/curl http://example.com/controller/method

使用/usr/local/bin/php而不是php来获取codeigniter来提取URI段。

所以尝试一下

/usr/local/bin/php /home/name/public_html/index.php stockupdate index

您可以将ob_start()放入配置文件中,以删除最后两个错误。

试试这个工作。

php /full-path-to-cron-file/cron.php /test/index

参考[ http://www.asim.pk/2009/05/14/creating-and-installing-crontabs-using-codeigniter/]

如此处所述: https : //ellislab.com/codeigniter/user-guide/general/cli.html,您应该获取控制器类名称和方法名称,并使用它们来构建CLI命令,如下所示:

php <path to your CI index.php> <name of the controller class> <name of the function> <optional arguments>

所以对于您的代码:

php /home/name/public_html/index.php stockupdate stock_update
wget -t=1 https:// codeginter.com/controller/method

t设置为尝试次数。

暂无
暂无

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

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