Reputation: 427
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";
}
}
}
Upvotes: 2
Views: 8529
Reputation: 1067
You can put ob_start()
in your config file to remove the last two errors.
Upvotes: 1
Reputation: 868
I used this commend
/usr/bin/curl http://example.com/controller/method
Upvotes: 3
Reputation: 2218
wget -t=1 https:// codeginter.com/controller/method
Set t
for the number of attempts.
Upvotes: 0
Reputation: 301
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
Upvotes: 4
Reputation: 14590
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
Upvotes: 0
Reputation: 986
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/]
Upvotes: 0