How to kill a node server using its pid(process id) that is running in background

Often we run node servers in background. Like using screen or using a cron job that starts while booting. So how do we kill it when when its running in the background.

Well, one way is if it is running in a screen we can resume the screen and terminate it.

Or even better we can find the process id of the server you are running. For this we need to remember the port on which you are running the node server.

For example if the server you want to terminate is running on the port 3029, run the below command to get the process id of that process.

lsof -i:8044

In the above command 8044 can be any port number. When we run the command we get output like below

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 108597 nagaraju 20u IPv6 318585 0t0 TCP *:8044 (LISTEN)

Notice the PID. That is what we need to terminate the process.

Now we run the below command to kill/terminate that process.

kill -9 108597

Using kill command to terminate the process and then checking if the process is still running after terminating it