I’m attempting to terminate a process through the command line associated with a specific port on my Ubuntu system.
When I execute the following command, it provides me with the port number:
sudo lsof -t -i:9001
So, my next step is to execute:
sudo kill 'sudo lsof -t -i:9001'
However, I encounter this error message:
ERROR: invalid process ID "lsof -t -i:9001".
Usage:
kill pid ... Send SIGTERM to every process listed.
kill signal pid ... Send a signal to every process listed.
kill -s signal pid ... Send a signal to every process listed.
kill -l List all signal names.
kill -L List all signal names in a nice table.
kill -l signal Convert between signal numbers and names.
I also attempted using: sudo kill 'lsof -t -i:9001'
Step 1: Identify the Process
When you need to terminate a process running on a particular port in Ubuntu, the first step is to identify the Process ID (PID) associated with that port. The command lsof
(short for “list open files”) can help us with this.
Open your terminal and type the following command, replacing 9001
with the port number you’re interested in:
sudo lsof -t -i:9001
The -t
flag tells lsof
to output only the PIDs of the processes, and the -i:9001
flag specifies that you want to target processes using the port 9001
.
This command will display the PID of the process currently using the specified port.
Step 2: Terminate the Process
Once you have the PID of the process, you can proceed to terminate it using the kill
command. The kill
command sends signals to processes, allowing you to control their behavior. In this case, we want to stop the process gracefully.
In the terminal, type the following command, replacing <PID>
with the actual PID you obtained from the previous step:
sudo kill <PID>
For example, if the PID is 12345
, the command would be:
sudo kill 12345
This command sends a default signal (SIGTERM
) to the process, asking it to gracefully terminate.
Step 3: Confirm Termination
After terminating the process, it’s a good idea to verify that it has indeed been stopped and the port is no longer in use. You can use the lsof
command again, like this:
sudo lsof -i:9001
If the process has been successfully terminated, you won’t see any output. This indicates that the port is now free and available for other uses.
In summary, to terminate a process running on a specific port in Ubuntu:
- Identify the process using the
lsof
command:
sudo lsof -t -i:9001
- Terminate the process using the
kill
command and the PID obtained:
sudo kill <PID>
- Confirm the termination by checking if the port is free:
sudo lsof -i:9001
Following these steps will help you gracefully terminate a process that’s using a particular port on your Ubuntu system.
let’s check more content on sixmedium