Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the local port a rails instance is running on?

Tags:

So I would like my Rails app instances to register themselves on a "I'm up" kind of thing I'm playing with, and I'd like it to be able to mention what local port it's running on. I can't seem to find how to do it - in fact just finding out its IP is tricky and needs a bit of a hack.

But no problem, I have the IP - but how can I find what port my mongrel/thin/webrick server is running on?

To be super explicit, if I start a rails app using script/server -p 3001, what can I do to pull that 3001 inside the app.

like image 506
RailFan Avatar asked Oct 12 '09 12:10

RailFan


People also ask

What is default rails port?

Now whenever the default port of 3000 is in use, it should automatically generate a new default server by incrementing the default port by 1 until there's an open port.

How do I stop a Rails server from running?

One way to shut down a rogue server is with the kill command. For the PID output above, you could kill the corresponding stuck server using kill 42612 . And if that didn't work, you could try kill -9 42612 .

What is the command to execute Rails server?

$ rails --help Usage: rails COMMAND [ARGS] The most common rails commands are: generate Generate new code (short-cut alias: "g") console Start the Rails console (short-cut alias: "c") server Start the Rails server (short-cut alias: "s") ... All commands can be run with -h (or --help) for more information.


1 Answers

You can call Rails::Server.new.options[:Port] to get the port that your Rails server is running on. This will parse the -p 3001 args from your rails server command, or default to port 3000.

like image 105
ndbroadbent Avatar answered Sep 18 '22 15:09

ndbroadbent