サーバースタートしたらエラー
miyajimayuki@miyajimayuukinoMacBook-Pro web_tech % ruby test.rb
[2020-03-17 14:25:54] INFO WEBrick 1.4.2
[2020-03-17 14:25:54] INFO ruby 2.6.3 (2019-04-16) [x86_64-darwin19]
Traceback (most recent call last):
11: from test.rb:2:in `<main>'
10: from test.rb:2:in `new'
9: from /Users/miyajimayuki/.rbenv/versions/2.6.3/lib/ruby/2.6.0/webrick/httpserver.rb:47:in `initialize'
8: from /Users/miyajimayuki/.rbenv/versions/2.6.3/lib/ruby/2.6.0/webrick/server.rb:108:in `initialize'
7: from /Users/miyajimayuki/.rbenv/versions/2.6.3/lib/ruby/2.6.0/webrick/server.rb:127:in `listen'
6: from /Users/miyajimayuki/.rbenv/versions/2.6.3/lib/ruby/2.6.0/webrick/utils.rb:65:in `create_listeners'
5: from /Users/miyajimayuki/.rbenv/versions/2.6.3/lib/ruby/2.6.0/socket.rb:762:in `tcp_server_sockets'
4: from /Users/miyajimayuki/.rbenv/versions/2.6.3/lib/ruby/2.6.0/socket.rb:227:in `foreach'
3: from /Users/miyajimayuki/.rbenv/versions/2.6.3/lib/ruby/2.6.0/socket.rb:227:in `each'
2: from /Users/miyajimayuki/.rbenv/versions/2.6.3/lib/ruby/2.6.0/socket.rb:764:in `block in tcp_server_sockets'
1: from /Users/miyajimayuki/.rbenv/versions/2.6.3/lib/ruby/2.6.0/socket.rb:201:in `listen'
/Users/miyajimayuki/.rbenv/versions/2.6.3/lib/ruby/2.6.0/socket.rb:201:in `bind': Address already in use - bind(2) for 0.0.0.0:3000 (Errno::EADDRINUSE)
原因
- サーバーをcontrol + Zで切ったら次にサーバーを立ち上げるとエラーになるようです。
解決
いま動いているport: 3000のサーバーをターミナルに出力して
% lsof -i:3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 2054 miyajimayuki 10u IPv6 0xb15f9119d06b6d43 0t0 TCP *:hbci (LISTEN)
ruby 2054 miyajimayuki 11u IPv4 0xb15f9119cf61f0cb 0t0 TCP *:hbci (LISTEN)
下記コマンドでプロセスを切る
% kill -9 {PID}
履歴
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 2054 miyajimayuki 10u IPv6 0xb15f9119d06b6d43 0t0 TCP *:hbci (LISTEN)
ruby 2054 miyajimayuki 11u IPv4 0xb15f9119cf61f0cb 0t0 TCP *:hbci (LISTEN)
miyajimayuki@miyajimayuukinoMacBook-Pro web_tech % kill -9 2054
miyajimayuki@miyajimayuukinoMacBook-Pro web_tech %
[1] + killed ruby test.rb
プロセスが切れたので再度サーバーが起動できるようになりました!
miyajimayuki@miyajimayuukinoMacBook-Pro web_tech % ruby test.rb
[2020-03-17 14:32:34] INFO WEBrick 1.4.2
[2020-03-17 14:32:34] INFO ruby 2.6.3 (2019-04-16) [x86_64-darwin19]
[2020-03-17 14:32:34] INFO WEBrick::HTTPServer#start: pid=2126 port=3000
以上です!