ターミナルを閉じてしまってRails sできない時の対処法

Railsプロジェクトを立ち上げたままターミナルを閉じてしまい、その後「rails s」コマンドを実行しても上手くいかないことってありますよね。

その時の対処法を一つ一つ丁寧に解説していきます。

ターミナルを誤って消した後に再度rails sするまでの手順

それでは一度rails sができなくなってから再度rails sできるようになるまでの手順を解説します。

「A server is already running.」を解消する

誤ってターミナルを閉じた後に”rails s”コマンドを実行したら、「A server is already running. Check /Users/asakura/KiZUKAI_project/kizukai_mvp/tmp/pids/server.pid.」と言われると思います。

意味は「すでにサーバが起動しています。/Users/yoshida/test_project/tmp/pids/server.pidを確認してください」とのこと。

$ rails s

WARNING: Use strings for Figaro configuration. 1 was converted to "1".
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
A server is already running. Check /Users/asakura/KiZUKAI_project/kizukai_mvp/tmp/pids/server.pid.
Exiting

この場合だと「/Users/yoshida/test_project/tmp/pids/」ディレクトリにserver.pidというファイルがあるかを確認します。

server.pidファイルが存在したらそれを削除します。

お使いのエディターから削除しちゃって大丈夫です。

「Address already in use」を解消する

再度「rails s」を実行すると、今度は別のエラーが発生します。なにやら最後の行で「Address already in use – bind(2) for “127.0.0.1” port 3000 (Errno::EADDRINUSE)」と言われてますね。

$ rails s

WARNING: Use strings for Figaro configuration. 1 was converted to "1".
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.5.1-p57), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
Exiting
Traceback (most recent call last):
	37: from bin/rails:3:in `<main>'
	36: from bin/rails:3:in `load'
	35: from /Users/asakura/KiZUKAI_project/kizukai_mvp/bin/spring:15:in `<top (required)>'
	34: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:70:in `require'
	33: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:70:in `require'
	32: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
	31: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
	30: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
	29: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
	28: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
	27: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
	26: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
	25: from /Users/asakura/KiZUKAI_project/kizukai_mvp/bin/rails:9:in `<top (required)>'
	24: from /Users/asakura/KiZUKAI_project/kizukai_mvp/bin/rails:9:in `require'
	23: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands.rb:18:in `<top (required)>'
	22: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/command.rb:46:in `invoke'
	21: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/command/base.rb:65:in `perform'
	20: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
	19: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
	18: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
	17: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:142:in `perform'
	16: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:142:in `tap'
	15: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:147:in `block in perform'
	14: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:53:in `start'
	13: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rack-2.0.7/lib/rack/server.rb:297:in `start'
	12: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/rack/handler/puma.rb:73:in `run'
	11: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/puma/launcher.rb:186:in `run'
	10: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/puma/single.rb:98:in `run'
	 9: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/puma/runner.rb:153:in `load_and_bind'
	 8: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/puma/binder.rb:90:in `parse'
	 7: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/puma/binder.rb:90:in `each'
	 6: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/puma/binder.rb:106:in `block in parse'
	 5: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/puma/binder.rb:266:in `add_tcp_listener'
	 4: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/puma/binder.rb:266:in `each'
	 3: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/puma/binder.rb:267:in `block in add_tcp_listener'
	 2: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/puma/binder.rb:273:in `add_tcp_listener'
	 1: from /Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/puma/binder.rb:273:in `new'
/Users/asakura/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/puma-3.12.1/lib/puma/binder.rb:273:in `initialize': Address already in use - bind(2) for "127.0.0.1" port 3000 (Errno::EADDRINUSE)

「3000番ポートはもうすでに使われてる」と言われているので、使用するポートを3000以外で指定してあげたら解決します。ポートの指定は-pオプションを使えばOKです。

例えば、ポートを3200で指定する場合は以下のように書きます。

$ rails s -p 3200

WARNING: Use strings for Figaro configuration. 1 was converted to "1".
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.5.1-p57), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3200
Use Ctrl-C to stop

どうしても3000ポートを使用したい場合の対処法

3000ポートを使用したいという場合でも大丈夫です。

まず「lsof -i:3000」コマンドで3000ポートの使用状況を確認します。

$ lsof -i:3000
COMMAND    PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ruby      2895 yoshida   12u  IPv4 0xf743c4c264e3fbe9      0t0  TCP localhost:hbci (LISTEN)
ruby      2895 yoshida   13u  IPv6 0xf743c4c25bcdd7e9      0t0  TCP localhost:hbci (LISTEN)
ruby      2895 yoshida   36u  IPv6 0xf743c4c25c119b09      0t0  TCP localhost:hbci->localhost:52269 (ESTABLISHED)
ruby      2895 yoshida   48u  IPv6 0xf743c4c25b9d54a9      0t0  TCP localhost:hbci->localhost:64645 (ESTABLISHED)

次にkillコマンドで現在3000ポートで使用されているプロセスを削除します。書き方は「kill -9 PID」です。

$ kill -9 2895

killコマンドを実行したら再度「lsof -i:3000」を実行してみましょう。先程のrubyプロセスは削除されているはずです。

これで3000ポートが使えるようになっているので、「rails s」を実行してみましょう。

$ rails s

WARNING: Use strings for Figaro configuration. 1 was converted to "1".
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.5.1-p57), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

しっかりとRailsのプロジェクトを立ち上げることができると思います!

タイトルとURLをコピーしました