CodeIgniter Forums

Full Version: Trying to create a cron job to run if a condition is true
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a simple database of product sheet requests in the administration section of our website. If a user requests a product sheet it gets sent to the database and an email gets sent to the people in charge of responding to the requests.

I want to create a cron job that runs every 24 hour and checks when the request was sent, if the request has been responded to, and if it hasnt been responded to within 24 hours it sends a reminder email to the people in charge of sending it, then it runs again at 48 hours and reminds them again, and the same with 72 hours.

Has anyone done anything like this before with CodeIgniter? I'm not sure how to begin. Any help you can offer is much appreciated!
Adding a cronjob that execute a specific function in CI is very simple. Here is an example of one running at 1am every day.

Code:
00 01 * * * php /path/to/codeigniter/index.php controller my_function >> /dev/null

The function your cron is executing should do all the work of checking if a reminder should be sent out or not.
Thanks for the reply,

I'm not sure what to use for my path to codeigniter in the cron job.
In my index.php I have my $application_folder = '../../codeI/application'; Is this what I should set as my path to codeigniter in the cron job also?

I think I did this right... here is my model...
Code:
<?php

class Model_reminders extends CI_Model{
   
   public function get_days_request_reminders($day){
    
    $day_start = date('Y-m-d 00:00:00', $day); //set the $day_start variable to the current date at 12:00 AM
    $day_end = date('Y-m-d 23:59:59', $day); //set the $day_end variable to the current date at 11:59 PM
    return $this->db->select('*')
        ->from('apps_requests')
        ->where('submit_time <', $day_start) //
        ->where('submit_time >', $day_end) //
        ->where('is_sent', 0)
        ->get()->result();
    
   }
   
   
}


and here is my controller... (obviously the email part needs some work still)
Code:
<?php
class Reminders extends CI_Controller{

   public function __construct(){
       parent::__construct();
       $this->load->library('input');
       $this->load->library('email');
       $this->load->model('Model_main');
   }
 
   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");
       $reminder = $this->Model_main->get_days_request_reminders($timestamp);
       
       if(!empty($reminder)){
           foreach($reminder as $r){
               $this->email->set_newline("\r\n");
               $this->email->to($r->email);
               $this->email->from("[email protected]");
               $this->email->subject("Request Reminder");
               $this->email->message("Request has not been responded to in 24 hours");
               $this->email->send();
           }
       }
   }
}


Again, what I'm trying to do is get all the requests that were sent yesterday and have not been marked as sent yet, and send an email out to the group responsible for responding to remind them.

Thanks for all your help!
(03-04-2015, 03:22 PM)alexandervj Wrote: [ -> ]I'm not sure what to use for my path to codeigniter in the cron job.
In my index.php I have my $application_folder = '../../codeI/application'; Is this what I should set as my path to codeigniter in the cron job also?

No, for the cronjob you'd need the full filepath up to and including "index.php". Cron doesn't know anything about your relative application path, and actually you don't want to use the application dir anyway...just the path TO index.php.

If you don't know the full path, you can open up index.php and scroll down towards the bottom, you will see a define('FCPATH', str_replace(SELF, '', __FILE__)); or something similar.
just under that line put die(FCPATH) and visit your site. It will give you the full path to your CI dir. Be sure to remove that line after you do this.

Something like /home/some_user/public_html/

Then just add an index.php to the end of that and that's what you'd want to use for your cronjob.
/home/some_user/public_html/index.php.
Thanks CroNiX,

I think I figured the cron part out but I think something is wrong with my controller and or model, not sure what. I got some of the code from a tutorial, and I'm actually not sure what the $day variable is in the model... here is the turorial...
http://glennstovall.com/blog/2013/01/07/...deigniter/
the script doesnt work when I run it in the address bar. It doesnt have any errors, just doesnt send an email so I'm guessing something is wrong with my controller or model
Did you remove the check for if it was a CLI request from your controllers index method? That would prevent it from running via the url.
Yeah, I did that, it seems to run it just doesn't send the email as expected. If I comment out everything and just have it send a test email it sends the email but there must be a problem with my controller or model or the way I went about this
I figured it out, I had the greater than less than signs backwards haha thanks for your help!
(03-05-2015, 06:56 AM)alexandervj Wrote: [ -> ]I figured it out, I had the greater than less than signs backwards haha thanks for your help!

Those are always the best! I once had an application with some code that said, if the value is positive then subtract it and if it is negative then add it - took me hours to work out why the negative values kept doubling up whilst the positives reset themselves to zero!

I believe NASA once lost a rocket due to the same error in a guidance control system Undecided
hahaha well if NASA did it that makes me feel not as bad!