Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a process by its pid on Linux? [closed]

I'm new to Linux and I'm building a program that receives the name of a process, gets its PID (I have no problem with that part) and then passes the PID to the kill command, but its not working.

It goes something like this:

read -p "Process to kill: " proceso
proid= pidof $proceso
echo "$proid"
kill $proid

Can someone tell me why it isn't killing it?

I know that there are some other ways to do it, even with the PID, but none of them seems to work for me. I believe it's some kind of problem with the Bash language (which I just started learning).

like image 529
Joel Flores Sottile Avatar asked Aug 31 '25 01:08

Joel Flores Sottile


2 Answers

use the following command to display the port and PID of the process:

sudo netstat -plten 

AND THEN

kill -9 PID

Here is an example to kill a process running on port 8283 and has PID=25334

enter image description here

like image 57
Tadele Ayelegn Avatar answered Sep 02 '25 16:09

Tadele Ayelegn


Instead of this:

proid= pidof $proceso

You probably meant this:

proid=$(pidof $proceso)

Even so, the program might not get killed. By default, kill PID sends the TERM signal to the specified process, giving it a chance to shut down in an orderly manner, for example clean up resources it's using. The strongest signal to send a process to kill without graceful cleanup is KILL, using kill -KILL PID or kill -9 PID.


I believe it's some kind of problem with the bash language (which I just started learning).

The original line you posted, proid= pidof $proceso should raise an error, and Bash would print an error message about it. Debugging problems starts by reading and understanding the error messages the software is trying to tell you.

like image 33
janos Avatar answered Sep 02 '25 16:09

janos