CodeIgniter Forums
Where to set CI_ENV for CI methods executed via cron? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Where to set CI_ENV for CI methods executed via cron? (/showthread.php?tid=67800)

Pages: 1 2


Where to set CI_ENV for CI methods executed via cron? - sneakyimp - 04-10-2017

I was delighted to learn that one can invoke CodeIgniter controller methods via CLI so I've constructed a variety of controller methods to handle jobs I want to run via cron. There's one big problem: cron doesn't read the CI_ENV that I added to my .bashrc file so I cannot set CI_ENV before my code starts executing.

I did find a workaround of sorts. Namely, you add the source ~/.bashrc command before each of my cronjobs:
Code:
* * * * * source ~/.bashrc; php /tmp/foo/foo.php

This is a complex project with perhaps a dozen cron jobs, so that's going to result in a LOT of extra junk in my crontab. Can someone suggest a better way?


RE: Where to set CI_ENV for CI methods executed via cron? - InsiteFX - 04-11-2017

You can set it in your .htaccess file.

Code:
    # Multiple Environment config, set this to development, testing or production
    #Set the CodeIgniter Environment.
    SetEnv CI_ENV development
    #SetEnv CI_ENV production



RE: Where to set CI_ENV for CI methods executed via cron? - spjonez - 04-11-2017

This is how I do it:

public/index.php

PHP Code:
/*
 *---------------------------------------------------------------
 * APPLICATION ENVIRONMENT
 *---------------------------------------------------------------
 *
 * You can load different configurations depending on your
 * current environment. Setting the environment also influences
 * things like logging and error reporting.
 *
 * This can be set to anything, but default usage is:
 *
 *     development
 *     testing
 *     production
 *
 * NOTE: If you change these, also change the error_reporting() code below
 */
 
if (php_sapi_name() == 'cli') {
     
define('ENVIRONMENT'$argv[$argc 1]);
 }
 else {
     
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
 } 

Calling it:

PHP Code:
php public/index.php controller method production 



RE: Where to set CI_ENV for CI methods executed via cron? - sneakyimp - 04-11-2017

(04-11-2017, 03:52 AM)InsiteFX Wrote: You can set it in your .htaccess file.
You didn't read my post at all, did you? I'm talking about CLI and cron jobs, not apache.


RE: Where to set CI_ENV for CI methods executed via cron? - sneakyimp - 04-11-2017

(04-11-2017, 06:59 AM)spjonez Wrote: This is how I do it:

public/index.php...

Calling it:

PHP Code:
php public/index.php controller method production 
Thank you for your response!

This does in fact address the problem but is not ideal for two reasons:
1) one must specify the environment every single time one specifies a cron/cli command. My crontab is going to have a dozen commands in it, all of which would need to have this value specified.
2) edits to the index.php file can make upgrading CodeIgniter more difficult because you have to propagate these changes manually merge these changes with any made by the CI core.

Is there not one place we can specify this value that works for both cron jobs and for cli execution?


RE: Where to set CI_ENV for CI methods executed via cron? - dave friend - 04-11-2017

You do not need to do anything to index.php as suggested by spjonez. Index.php already looks at $_SERVER['CI_ENV'] and responds accordingly.

As InsiteFX suggested set .htaccess like this

Code:
   #Set the CodeIgniter Environment.
   SetEnv CI_ENV production

So a command along the lines ought to work.

... php full/path/to/html/root/index.php controller method arg1 argn

The big thing you have to assert is that your server has the env module up an running. It is often not on in shared hosting situations.


RE: Where to set CI_ENV for CI methods executed via cron? - InsiteFX - 04-11-2017

I did read it and if you check a it still runs through index.php and also .htaccess.


RE: Where to set CI_ENV for CI methods executed via cron? - Paradinight - 04-11-2017

(04-11-2017, 03:32 PM)InsiteFX Wrote: I did read it and if you check a it still runs through index.php and also .htaccess.

No. If you call the php on the cli, the apache process never called and the .htaccess file never parsed...


RE: Where to set CI_ENV for CI methods executed via cron? - Diederik - 04-11-2017

Or you could just run wget with a url as a cron.


RE: Where to set CI_ENV for CI methods executed via cron? - InsiteFX - 04-12-2017

This is done on the shell command line

env VAR1="blahblah" command_to_run command_options