2

I'm making a Cron job in CodeIgniter and I need access to my models in order to perform it's tasks. Here's my controller code.

<?php
    class Cron extends CI_Controller
    {
        public function send_mail() {
            if ( PHP_SAPI !== 'cli' ) exit('No web access allowed');
            echo "Hello World";
        }
    }

And if I run this command on the terminal

php /home/path/to/project/index.php cron send_mail

It doesn't return anything, it's not executing the function. But if I delete the database and session libraries from the autoload.php file, it runs perfectly. Any ideas?

CatBrownie
  • 150
  • 3
  • 11
  • Did you get any error while function not executing?? – Indrasinh Bihola Nov 25 '15 at 05:24
  • Nothing at all, is there a way to debug or log my application? – CatBrownie Nov 25 '15 at 06:29
  • Make changes in root **index.php** to make CI in **development mode**. – Indrasinh Bihola Nov 25 '15 at 06:30
  • Check that you aren't using any environment variables within your code. If you are you will have to set them before you call php using: ENV_VARIABLE=variable value php script.php. This is do to the fact that environment variables that are set within your http server do not get set when executing code on the cli as you are not using your http server to run the script. – whitwhoa Nov 25 '15 at 15:03
  • @IndrasinhBihola You mean the line ~21 that says `define('ENVIRONMENT', 'development');` Yeah, it's like that. – CatBrownie Nov 25 '15 at 20:32
  • @nullReference I'm using `define('ENVIRONMENT', 'development');` as the environment variable, should I define it as `production`? – CatBrownie Nov 25 '15 at 20:36
  • Negative. That is defining a php constant. I am talking about variables that are set within your http server that are commonly accessed via $_SERVER['some_var'], or get_env('some_var'). It is common practice to define the paths to your application and system directories using environment variables as paths may differ from dev/test/prod. I would scan your codebase for $_SERVER and get_env(. Also you should be able to check your system logs for errors pertaining to this delima. – whitwhoa Nov 25 '15 at 21:51
  • @nullReference Where can I check the logs? – CatBrownie Nov 25 '15 at 22:14
  • Yes I am talking about `define('ENVIRONMENT', 'development');` dont change it to production it's correct. – Indrasinh Bihola Nov 26 '15 at 03:55
  • 1
    Check if there is any 'hook' added for checking session and redirecting – Syam Nov 26 '15 at 08:30
  • umm, I wonder about this, but isn't `session` only applies to web-browser? then, when web application want to send `cookies` to you terminal, it won't be able to do anything with it. That's why CI stops executing because you cannot be identified as a web-browser? – Raynal Gobel Nov 26 '15 at 10:39

0 Answers0