Skip to content

How to kill a process by its pid in linux

I’m new in 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 pass 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).



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.

User contributions licensed under: CC BY-SA
9 People found this is helpful