Welcome Guest, Not a member yet? Register   Sign In
Help with cronjob command line
#1

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
Reply
#2

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

(This post was last modified: 07-17-2016, 10:09 AM by albertleao. Edit Reason: Added example )

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 /
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#4

(This post was last modified: 07-17-2016, 10:14 AM by doomie22.)

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.
Reply
#5

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

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);
 
     }
 
 }
 
 }

Reply
#7

Perhaps your appointments model is not returning any appointments.
Reply
#8

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); 
Reply
#9

(This post was last modified: 07-17-2016, 09:39 PM by PaulD.)

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB