How to check whether my Rails app is running with a dev or production environment while running
Asked Answered
R

2

6

I'm running a rails app on a Centos 6.5 server with Passenger and Nginx. How can I check which environment it's running on without stopping it?

Rembert answered 18/8, 2015 at 14:28 Comment(0)
F
10

Use the passenger-status command. For example, this shows me passenger is running the production environment (the first line under the Application groups heading):

(production-web) ubuntu@ip-10-0-3-146 ~% sudo passenger-status                                                              
Version : 5.0.15
Date    : 2015-08-20 17:40:24 +0000
Instance: lNNFwV1C (Apache/2.4.7 (Ubuntu) Phusion_Passenger/5.0.15)

----------- General information -----------
Max pool size : 12
App groups    : 1
Processes     : 6
Requests in top-level queue : 0

----------- Application groups -----------
/home/my-app/deploy/current (production):
  App root: /home/my-app/deploy/current
  Requests in queue: 0
  * PID: 11123   Sessions: 0       Processed: 12997   Uptime: 21h 14m 2s
    CPU: 0%      Memory  : 190M    Last used: 1s ago
  * PID: 11130   Sessions: 0       Processed: 140     Uptime: 21h 14m 2s
    CPU: 0%      Memory  : 153M    Last used: 9m 32s a
  * PID: 11137   Sessions: 0       Processed: 15      Uptime: 21h 14m 2s
    CPU: 0%      Memory  : 103M    Last used: 57m 54s
  * PID: 11146   Sessions: 0       Processed: 6       Uptime: 21h 14m 2s
    CPU: 0%      Memory  : 101M    Last used: 7h 47m 4
  * PID: 11153   Sessions: 0       Processed: 5       Uptime: 21h 14m 1s
    CPU: 0%      Memory  : 100M    Last used: 8h 42m 3
  * PID: 11160   Sessions: 0       Processed: 2       Uptime: 21h 14m 1s
    CPU: 0%      Memory  : 81M     Last used: 8h 42m 3

rails console is not reliable - it only tells you what environment the console is running under. Passenger may be configured to run in a different environment.

Farro answered 20/8, 2015 at 17:47 Comment(1)
It appears this will only show the application group after a request. If nginx has just been restarted, then passenger isn't running any application groups yet.Classieclassification
M
8

Your environment is found on Rails.env.

Loading development environment (Rails 4.2.3)
2.1.2 :001 > Rails.env
 => "development" 

You can also use the environment in a question format for conditionals:

2.1.2 :002 > Rails.env.production?
 => false 
2.1.2 :003 > Rails.env.pickle?
 => false 
2.1.2 :004 > Rails.env.development?
 => true 

Word of warning - this is if you want to program something within your code that checks the environment.

Mattingly answered 18/8, 2015 at 14:29 Comment(4)
Thanks, I'll set it as correct as soon as it'll let me.Rembert
Howeber be careful on one thing => you have to execute this command on rails console. However, the rails console can be launch on another environment that your Passenger rails application (since the environment can be passed as an environment variable).Fiddlewood
@RaphaelPr I demonstrated its usage with the Rails console, but it's accurate when used in controllers, views, helpers, etc.Mattingly
Yes, just to say that if the application is up and running, you might not be able to add this code without having to restart your app.Fiddlewood

© 2022 - 2024 — McMap. All rights reserved.