Welcome Guest, Not a member yet? Register   Sign In
Where to set CI_ENV for CI methods executed via cron?
#1

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

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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 04-11-2017, 06:59 AM by spjonez.)

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

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

(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?
Reply
#6

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

I did read it and if you check a it still runs through index.php and also .htaccess.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

(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...
Reply
#9

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

This is done on the shell command line

env VAR1="blahblah" command_to_run command_options
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB