Where to set CI_ENV for CI methods executed via cron? |
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?
You can set it in your .htaccess file.
Code: # Multiple Environment config, set this to development, testing or production What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
This is how I do it:
public/index.php PHP Code: /* Calling it: PHP Code: php public/index.php controller method production (04-11-2017, 06:59 AM)spjonez Wrote: This is how I do it: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?
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. 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.
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 )
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 )
|
Welcome Guest, Not a member yet? Register Sign In |