<p>Alternatively, you could write your own yiic shell wrapper for each environment that defined <code>CRAFT_ENVIRONMENT</code>.</p>

<p>Something like <code>yiicstaging</code>:</p>

<pre><code>#!/usr/bin/env php
&lt;?php

define('CRAFT_ENVIRONMENT', 'staging')
require_once('path/to/craft/app/etc/console/yiic.php');
</code></pre>

CC BY-SA 3.0
2017-01-06T22:16:26.323Z
That was going to be my next attempt. Thanks! Might be a nice feature in the future to be able to handle this natively.
Jeremy Gimbel
CC BY-SA 3.0
2017-01-06T17:52:41.803Z
2017-01-06T17:52:41.803Z
Brad Bell
<p>I have built a plugin with some console commands for automating some regular tasks that will be run by a cron job for my site.</p>

<p>In order to get the commands working, I had to replicate my database configuration for the <code>console</code> environment in <code>db.php</code>.</p>

<p>All was well until I realized that each of my environments, local, staging and production will have unique database credentials, but there is only one console environment.</p>

<p>Is there a way I can force the <code>yiic</code> command to use a specific environment via a command line switch, either passing the domain name of an existing environment or additional additional arbitrarily named console environments? </p>

<p>Or is there another way to accomplish this so that the console environment has a unique configuration for each copy of the site?</p>

<hr>

<p>For the time being I've been able to add an <code>--environment</code> switch to <code>craft/app/etc/console/yiic.php</code> which defines the <code>CRAFT_ENVIRONMENT</code> constant ahead of it defaulting to <code>console</code>, but this seems rather hacky and I'd hope there's a better way to do this.</p>

<pre><code>// Existing switch check starting on line 18
if (isset($_SERVER['argv']))
{
    foreach ($argv as $key =&gt; $arg)
    {
        if (strpos($arg, '--configPath=') !== false)
        {
            $parts = explode('=', $arg);
            $frontConfigPath = rtrim($parts[1], '/').'/';
            unset($_SERVER['argv'][$key]);
            break;
        }

        // New environment switch added here
        if (strpos($arg, '--environment=') !== false)
        {
            $parts = explode('=', $arg);
            $environment = $parts[1];
            define('CRAFT_ENVIRONMENT', $environment);
            unset($_SERVER['argv'][$key]);
            break;
        }
        // end new environment switch
    }
}
</code></pre>

<multi-environment><environments><console><cli><yiic>
How do I configure multiple environments for console commands?
# How do I configure multiple environments for console commands?

(Asked by: username_0 on 2017-01-06)

I have built a plugin with some console commands for automating some regular tasks that will be run by a cron job for my site.
In order to get the commands working, I had to replicate my database configuration for the console environment in db.php.
All was well until I realized that each of my environments, local, staging and production will have unique database credentials, but there is only one console environment.
Is there a way I can force the yiic command to use a specific environment via a command line switch, either passing the domain name of an existing environment or additional additional arbitrarily named console environments? 
Or is there another way to accomplish this so that the console environment has a unique configuration for each copy of the site?

For the time being I've been able to add an --environment switch to craft/app/etc/console/yiic.php which defines the CRAFT_ENVIRONMENT constant ahead of it defaulting to console, but this seems rather hacky and I'd hope there's a better way to do this.
// Existing switch check starting on line 18
if (isset($_SERVER['argv']))
{
    foreach ($argv as $key => $arg)
    {
        if (strpos($arg, '--configPath=') !== false)
        {
            $parts = explode('=', $arg);
            $frontConfigPath = rtrim($parts[1], '/').'/';
            unset($_SERVER['argv'][$key]);
            break;
        }

        // New environment switch added here
        if (strpos($arg, '--environment=') !== false)
        {
            $parts = explode('=', $arg);
            $environment = $parts[1];
            define('CRAFT_ENVIRONMENT', $environment);
            unset($_SERVER['argv'][$key]);
            break;
        }
        // end new environment switch
    }
}

**Tags:** multi-environment, environments, console, cli, yiic

---

## 1 answers

### Answer by username_1 on 2017-01-06. Score: 3 | Accepted answer

Alternatively, you could write your own yiic shell wrapper for each environment that defined CRAFT_ENVIRONMENT.
Something like yiicstaging:
#!/usr/bin/env php

> **Comments:**
> **username_0**: That was going to be my next attempt. Thanks! Might be a nice feature in the future to be able to handle this natively.  

---

