sajad torkamani

Check what process is running on a port

Use lsof to find the process:

sudo lsof -i :<port>

This will display the list of processes running on <port>. For example, if we have MySQL installed and we haven't changed the default port of 3306, then running lsof -i :3306 should show:

COMMAND  PID  USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
mysqld  1078 sajad   32u  IPv4 0x603ffdfb7996c161      0t0  TCP localhost:mysql (LISTEN)

Or, if we're using Postgres and we run lsof -i :5432:

COMMAND   PID  USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
postgres 1133 sajad    7u  IPv6 0x603ffdfb784aae71      0t0  TCP localhost:postgresql (LISTEN)
postgres 1133 sajad    8u  IPv4 0x603ffdfb70f4a2e9      0t0  TCP localhost:postgresql (LISTEN)

Kill the process

Once we have the process ID (PID) from the lsof command, you can terminate the process using the kill command:

sudo kill -9 <pid>

kill sends a signal to the process. There are a bunch of numbers that represent different signals and 9 represents the KILL signal. Here are some common signals:

 1       HUP (hang up)
 2       INT (interrupt)
 3       QUIT (quit)
 6       ABRT (abort)
 9       KILL (non-catchable, non-ignorable kill)
 14      ALRM (alarm clock)
 15      TERM (software termination signal)
Tagged: Unix