如何在服务器

时间:2015-08-24 10:50:35

标签: php codeigniter crontab cpanel

您好我正在尝试使用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";
            }
        }
    }

5 个答案:

答案 0 :(得分:3)

我用这个推荐

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

答案 1 :(得分:1)

您可以在配置文件中添加ob_start()以删除最后两个错误。

答案 2 :(得分:0)

尝试这项工作。

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

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

答案 3 :(得分:0)

如前所述: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

答案 4 :(得分:0)

wget -t=1 https:// codeginter.com/controller/method

设置t的尝试次数。