title: How to setup cron job for a codeigniter script on server
body: <p>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.</p>
  
  <p>Script Path: /home/name/public_html/application/modules/stockupdate/controllers/stockupdate.php</p>
  
  <p>I need to run this script every hour I have read tutorials and I have tried to to use following commands:</p>
  
  <pre><code>1) php /home/name/public_html/index.php stockupdate 
  2)/home/name/public_html/application/modules/stockupdate/controllers/stockupdate.php
  </code></pre>
  
  <p>First one sends the following error:</p>
  
  <pre><code>&lt;h4&gt;A PHP Error was encountered&lt;/h4&gt;
  
  &lt;p&gt;Severity: Notice&lt;/p&gt;
  &lt;p&gt;Message:  Undefined index: REMOTE_ADDR&lt;/p&gt;
  &lt;p&gt;Filename: core/Input.php&lt;/p&gt;
  &lt;p&gt;Line Number: 351&lt;/p&gt;
  
  &lt;h4&gt;A PHP Error was encountered&lt;/h4&gt;
  
  &lt;p&gt;Severity: Warning&lt;/p&gt;
  &lt;p&gt;Message:  Cannot modify header information - headers already sent by (output started at /home/name/public_html/system/core/Exceptions.php:185)&lt;/p&gt;
  &lt;p&gt;Filename: libraries/Session.php&lt;/p&gt;
  &lt;p&gt;Line Number: 689&lt;/p&gt;
  
  &lt;h4&gt;A PHP Error was encountered&lt;/h4&gt;
  
  &lt;p&gt;Severity: Warning&lt;/p&gt;
  &lt;p&gt;Message:  Cannot modify header information - headers already sent by (output started at /home/name/public_html/system/core/Exceptions.php:185)&lt;/p&gt;
  &lt;p&gt;Filename: helpers/url_helper.php&lt;/p&gt;
  &lt;p&gt;Line Number: 542&lt;/p&gt;
  </code></pre>
  
  <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</p>
  
  <p>stockupdate.php</p>
  
  <pre><code>&lt;?php
  class StockUpdate extends Backend_Controller
  {
      public function __construct()
      {
          parent::__construct();
          $this-&gt;load-&gt;library('ftp');
          $this-&gt;load-&gt;library('csvimport');
          $this-&gt;load-&gt;model('stock_update_model');
          ini_set('memory_limit', '3000M');
          set_time_limit(0); 
      }
  
      function index()
      {
          if ($this-&gt;input-&gt;is_cli_request())
          {
              $this-&gt;stock_update_model-&gt;truncate_stock();
              $this-&gt;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-&gt;stock_update_model-&gt;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";
              }
          }
      }
  </code></pre>