Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Zend APPLICATION_ENV for Command Line Scripts

The standard practice for setting the environment (production/staging/development) with the Zend Framework is using SetEnv in the Apache config (or .htaccess) to set the APPLICATION_ENV. This obviously works well for web requests, but what about running command line scripts (that are still part of the application, using the same bootstrapping, and rely on the correct APPLICATION_ENV)?

Any best practices for that? Right now I'm just dropping a .environment.php file in my cli directory - it's included if it exists (similar to .htaccess I guess), and can be used to set the environment.

like image 808
Tim Lytle Avatar asked Oct 16 '11 01:10

Tim Lytle


2 Answers

From a terminal

For (linux)command-line only usage you could add following line to your ~/.bashrc

export APPLICATION_ENV=development

Or add the line to /etc/profile to set the environment variable for all users.

For more information:

  • Howto: Set environment variables in Windows 7
  • Howto: Set environment variables in Linux

From a crontab

Cron uses a clean/empty environment and doesn't look at /etc/profile or ~/.bashrc for the APPLICATION_ENV or other environment variables.

Just define the environment variable inside the crontab:

APPLICATION_ENV=development
0 0 * * *   /usr/bin/php /path/to/your_script.php

For more information:

  • Cron and Crontab usage and examples
like image 52
Bob Fanger Avatar answered Oct 14 '22 23:10

Bob Fanger


You can also run the script

APPLICATION_ENV="local" php cli.php
like image 40
Arun Killu Avatar answered Oct 15 '22 00:10

Arun Killu