How to Kill a Process Using a Port in Linux

How to Kill a Process Using a Port in Linux

When running applications on Linux, you may find that something you're trying to launch can't because a process is already using the port that your new application needs. This is commonly the case with web applications. Another possibility is that a process has become stuck or unresponsive. This can happen for a variety of reasons, including software bugs or system crashes.

In these cases, it may be necessary to kill the process in order to free up the resources it's using. One way to do this is by using the process's port number.

Killing the Process

If we know that we want to free up the port 8080, for example, then it'd help if we can find the process using the port number. First we'll need to find the process ID (PID) associated with the port. You can do this using the lsof command, which lists all the open files on the system. To find the PID associated with a specific port, use the -i flag to specify the port number and the -t flag to only show the PIDs:

$ lsof -i :<port number> -t

For example, if the process is using port 8080, you would use the following command:

$ lsof -i :8080 -t

This will print the PID of the process using that port. Once you have the PID, you can then use the kill command to terminate the process. The kill command sends a signal to the process, which can either terminate it or allow it to clean up before exiting.

To kill the process immediately, use the -9 flag:

$ kill -9 <pid>

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

Be sure to replace <pid> with the PID of the process you want to kill. For example, if the PID is 1234, you would use the following command:

$ kill -9 1234

This will immediately kill the process associated with that PID.

SIGKILL

In some cases, the kill command may not be able to terminate the process. This can happen if the process is running with special privileges, such as those granted to system processes.

In these cases, you can try using the killall command, which sends a signal to all processes with a given name. To use killall, specify the name of the process you want to kill and the signal to send.

SIGKILL is a Unix signal that is used to immediately terminate a process. The signal cannot be caught or ignored, and it is not possible for the process to clean up any resources or perform any other actions before it exits.

Note: The signal is typically used as a last resort when a process is unresponsive or stuck, and other methods of terminating the process have failed.

For example, to kill a process named my_process using the SIGKILL signal, you would use the following command:

$ killall -s SIGKILL my_process

This will attempt to kill all processes with the name my_process, regardless of their PID or privileges.

Conclusion

Killing a process using a port in Linux involves finding the PID of the process using the lsof command, and then using the kill or killall command to terminate the process. This can be a useful tool for freeing up resources and troubleshooting unresponsive processes.

Last Updated: December 13th, 2022
Was this helpful?

Make Clarity from Data - Quickly Learn Data Visualization with Python

Learn the landscape of Data Visualization tools in Python - work with Seaborn, Plotly, and Bokeh, and excel in Matplotlib!

From simple plot types to ridge plots, surface plots and spectrograms - understand your data and learn to draw conclusions from it.

Want a remote job?

    © 2013-2023 Stack Abuse. All rights reserved.

    DisclosurePrivacyTerms