seeb4coding_post_placeholder

How to Terminate a Server Process Running on a Specific Port (Windows & Mac)

When a server or process is occupying a specific port, sometimes you need to terminate it to free up the port or stop the service. The process to do this varies slightly between Windows and macOS. Here’s a step-by-step guide for both platforms.


On Windows

Step 1: Find the Process ID (PID) Using the Desired Port

To find which process is using a particular port, such as port 8081, use the netstat command in the Command Prompt.

Open Command Prompt as Administrator.

Run the following command:

netstat -ano | findstr :8081 

Example output:

  TCP    0.0.0.0:8081           0.0.0.0:0              LISTENING       35248
  TCP    [::]:8081              [::]:0                 LISTENING       35248

The PID (Process ID) is 35248 in this example.

Step 2: Kill the Process Using the PID

Once you know the PID of the process, you can terminate it using the taskkill command:

taskkill /PID 35248 /F

This will forcefully terminate the process with PID 35248. You should see a success message like:

SUCCESS: The process with PID 35248 has been terminated.

Step 3: Verify the Process is Terminated

To ensure the process is no longer running, run the netstat command again:

netstat -ano | findstr :8081

If the process has been terminated, there will be no output for that port.


On macOS

Step 1: Find the Process ID (PID) Using the Desired Port

To identify which process is using a port (e.g., 8081), use the lsof command:

Open the Terminal.

Run the following command:

lsof -i :8081 

Example output:

COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node     1234   user   12u  IPv6 0x23ad33110b12bd7b      0t0  TCP *:8081 (LISTEN)

The PID here is 1234.

Step 2: Kill the Process Using the PID

Once you have the PID, use the kill command to terminate the process:

kill -9 1234

The -9 option forcefully kills the process.

Step 3: Verify the Process is Terminated

To confirm the process has been terminated, run the lsof command again:

lsof -i :8081

If the process has been terminated, no output will be shown for that port.


Summary

Windows:

Find PID:

netstat -ano | findstr :<port>

Kill Process:

taskkill /PID <PID> /F

macOS:

Find PID:

lsof -i :<port>

Kill Process:

kill -9 <PID>

This approach works well for terminating processes on specific ports, freeing them for other services.

Leave a Reply

Your email address will not be published. Required fields are marked *