简体   繁体   中英

How to setup cron job for a codeigniter script on server

Hi I am trying to setup cron job for a script on server using cpanel however I have never done this and it would really help if someone can guide me.

Script Path: /home/name/public_html/application/modules/stockupdate/controllers/stockupdate.php

I need to run this script every hour I have read tutorials and I have tried to to use following commands:

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

First one sends the following error:

<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>

The second command send me error that access denied. I would really appreciate if someone can guide me to create cron job please. Thank you

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

Use /usr/local/bin/php instead of php to get codeigniter to pick up on the URI segments.

so try this if it works

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

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

Try this it's work.

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

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

As stated here: https://ellislab.com/codeigniter/user-guide/general/cli.html you should get the controller class name and the method name and use them to build the CLI command like this:

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

so for your code:

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

t设置为尝试次数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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