CodeIgniter Forums
Help with cronjob command line - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Help with cronjob command line (/showthread.php?tid=65731)



Help with cronjob command line - doomie22 - 07-17-2016

Hi all,

I am trying to run a controller that I have made for a test (to test cronjob) and I am just struggling at the command line.  I have the following controller

PHP Code:
<?php 

class Reminders extends CI_Controller { public function __construct() { parent::__construct(); 

 
   $this->load->library('input');
 
   $this->load->library('email');
 
   $this->load->model('Appointment_model');
 
 }
 
 public function index()
 
 {
 
   if(!$this->input->is_cli_request())
 
 {
 
     echo "This script can only be accessed via the command line" PHP_EOL;
 
     return;
 
 }
 
 $timestamp strtotime("+1 days");
 
 $appointments $this->Appointment_model->get_days_appointments($timestamp);
 
 if(!empty($appointments))
 
 {
 
     foreach($appointments as $appointment)
 
     {
 
         $this->email->set_newline("\r\n");
 
         $this->email->to($appointment->email);
 
         $this->email->from("[email protected]");
 
         $this->email->subject("Appointment Reminder");
 
         $this->email->message("You have an appointment tomorrow");
 
         $this->email->send();
 
         $this->Appointment_model->mark_reminded($appointment->id);
 
     }
 
 }
 
 }


and I have a Model that is

PHP Code:
<?php

class Appointment_model extends CI_Model
{
 
 public function get_days_appointments($day)
 
 {
 
   $day_start date('Y-m-d 00:00:00'$day);
 
   $day_end date('Y-m-d 23:59:59'$day);
 
   return $this->db->select('*')
 
     ->from('appointments')
 
     ->where('start_time <'$day_start) ->where('start_time >'$day_end)
 
     ->get()->result();
 
      
  
}
 
 
 public function mark_reminded($appointment_id)
 
 {
 
   return $this->db->where('id'$appointment_id)->update('appointments', array('is_reminded' => 1));
 
 }



The example I was given for the cronjob was 

0 13 * * * php [application_path]/index.php cli/reminders

Which states that it will run at 1pm each day.

So I set mine to run a few minutes after using the following.

24 16 * * * php /home/[account_name]/public_html/testing/index.php cli/reminders

But all it did was run the main page of my site.  Thing is I am using the system that it doesn't have the index.php show up when it comes to the pages.

Can anyone help me with the command line to access this controller to run it please.?
Thanks,

Doomie


RE: Help with cronjob command line - doomie22 - 07-17-2016

All it is doing is running the the main page.  Can anyone help


RE: Help with cronjob command line - albertleao - 07-17-2016

You're not running the command correctly: http://www.codeigniter.com/user_guide/general/cli.html

Code:
$ cd /path/to/project;

$ php index.php tools message


The controller and method are separated by a space not a /


RE: Help with cronjob command line - doomie22 - 07-17-2016

I had the controller in a folder called cli, so I moved it in to the main controller folder and changed it to this.

php /home/[account_name]/public_html/testing/index.php reminders

It still sends the report of the standard main page html to me in the cron job report.

P.S. I am running this through cPanel Cron Jobs on my hosting.


RE: Help with cronjob command line - arma7x - 07-17-2016

Have you try command php-cli, it's work for me to run cronjob in cpanel.


RE: Help with cronjob command line - doomie22 - 07-17-2016

Thank you for all the help.

It seems to be running as if I just try to send an email with simple code it comes through, but what seems to happen atm is this code is running but its not doing anything, but I think it could be because it is not getting to the database.  Can someone take a look at this please.

PHP Code:
<?php 

class Reminders extends CI_Controller { public function __construct() { parent::__construct(); 
 
 
  
  $this
->load->library('email');
 
 $this->load->model('Appointment_model');
 
 
  
}
 
 public function run()
 
 {
 
 $timestamp strtotime("+1 days");
 
 $appointments $this->Appointment_model->get_days_appointments($timestamp);
 
 if(!empty($appointments))
 
 {
 
     foreach($appointments as $appointment)
 
     {
 
         $this->email->set_newline("\r\n");
 
         $this->email->to($appointment->email);
 
         $this->email->from("[email protected]");
 
         $this->email->subject("Appointment Reminder");
 
         $this->email->message("You have an appointment tomorrow");
 
         $this->email->send();
 
         $this->Appointment_model->mark_reminded($appointment->id);
 
     }
 
 }
 
 }




RE: Help with cronjob command line - PaulD - 07-17-2016

Perhaps your appointments model is not returning any appointments.


RE: Help with cronjob command line - doomie22 - 07-17-2016

Ok, after much messing and testing, I have got it to work but there is just something wrong with 4 lines.

in the controller its

PHP Code:
$timestamp strtotime("+1 days");
$appointments $this->Appointment_model->get_days_appointments($timestamp); 

in the model

PHP Code:
$this->db->where('start_time < '$day_start);
$this->db->where('start_time > '$day_end); 


If I take out the where in the model it all works perfectly (for a test), I add this to just get the next day appointments and nothing happens as it cannot find any (even though there is one on every day for the next 6 days).

Can anyone see what I am missing with it as the time system has always been my weak point.

Just incase, here is the day_start and day_end incase its theses too.

PHP Code:
$day_start date('Y-m-d 00:00:00'$day);
$day_end date('Y-m-d 23:59:59'$day); 



RE: Help with cronjob command line - PaulD - 07-17-2016

You are asking it to be less than when the day starts, and greater than when the day ends, that surely will not return anything

Change
PHP Code:
$this->db->where('start_time < '$day_start);
$this->db->where('start_time > '$day_end); 

To
PHP Code:
$this->db->where('start_time > '$day_start);
$this->db->where('start_time < '$day_end); 

Best wishes,

Paul