Search code examples
rubylinuxbashshellport

Trying to kill process on port 8000 confusion


I am writing a Ruby script that deploys a server on port 8000 in the background, and then in the foreground I issue queries to the server. After I've issued my queries I kill the server, however when I kill the server, it seems to be switching ports.

I am doing it the following way in the ruby script:

To see PID that is running on port 8000:

lsof -i:8000 -t

Result:

RUNNING ON PORT 8000: COMMAND   PID         USER   FD   TYPE DEVICE   SIZE/OFF NODE NAME
java    26364 user1   84u  IPv6 199069      0t0  TCP *:8000 (LISTEN)

To kill the server I issue the command:

kill 26364

I then see if anything is running on port 8000:

# check if killed
lsof -i:8000 -t

Result:

  RUNNING ON PORT 8000: COMMAND   PID         USER   FD   TYPE      DEVICE SIZE/OFF NODE NAME
  ruby    25560 user1   58u  IPv4 199123      0t0  TCP                localhost:45789->localhost:8000 (ESTABLISHED)
  java    26364 user1   84u  IPv6 199069      0t0  TCP *:8000 (LISTEN)
  java    26364 user1   85u  IPv6 199124      0t0  TCP   localhost:8000->localhost:45789 (ESTABLISHED)

I only want to kill the process that is listening on port 8000, and keep my ruby script running.

Can someone please tell me what is going on? Why is it switching ports? How can I only kill my server port?


Solution

  • It doesn't look to me like it's switching ports; it's still listening on port 8000. It looks to me like two things are happening:

    1. The java process (PID 26364) is catching or ignoring the kill signal (SIGTERM), and continuing to listen on port 8000.
    2. A ruby process (PID 25560) is making a connection to localhost:8000 (from port 45789, which was probably dynamically allocated). That is, ruby is making a normal connection to the server on port 8000.

      Note that the java process owns the port 8000 end of the localhost:8000<->localhost:45789 TCP session, and the ruby process owns the port 45789 end.

    Whether the ruby process's connection is somehow a result of the kill signal, or just something it happened to do at about the same time, I couldn't tell you.