How to kill a process by its pid in linux
Asked Answered
D

9

21

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).

Draff answered 3/10, 2015 at 19:41 Comment(0)
D
8

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.

Ddene answered 3/10, 2015 at 19:44 Comment(1)
Thanks a lot guys! I tried all of your solutions but the problem was right there at what you pointed. Didn't know I had to put all that expression beetween ()'s and with $ at the beginning. I'll be more concerned about it now, thanks!Draff
U
31

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

Usia answered 4/10, 2020 at 6:39 Comment(0)
D
8

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.

Ddene answered 3/10, 2015 at 19:44 Comment(1)
Thanks a lot guys! I tried all of your solutions but the problem was right there at what you pointed. Didn't know I had to put all that expression beetween ()'s and with $ at the beginning. I'll be more concerned about it now, thanks!Draff
C
2

kill expects you to tell it **how to kill*, so there must be 64 different ways to kill your process :) They have names and numbers. The most lethal is -9. Some interesting ones include:

  • SIGKILL - The SIGKILL (also -9) signal forces the process to stop executing immediately. The program cannot ignore this signal. This process does not get to clean-up either.
  • SIGHUP - The SIGHUP signal disconnects a process from the parent process. This an also be used to restart processes. For example, "killall -SIGUP compiz" will restart Compiz. This is useful for daemons with memory leaks.
  • SIGINT - This signal is the same as pressing ctrl-c. On some systems, "delete" + "break" sends the same signal to the process. The process is interrupted and stopped. However, the process can ignore this signal.
  • SIGQUIT - This is like SIGINT with the ability to make the process produce a core dump.
Crucify answered 3/10, 2015 at 19:50 Comment(0)
B
0

You have to send the SIGKILL flag with the kill statement. kill -9 [pid] If you don't the operating system will choose to kill the process at its convenience, SIGKILL (-9) will tell the os to kill the process NOW without ignoring the command until later.

Barley answered 3/10, 2015 at 19:42 Comment(0)
E
0

Try this kill -9 It will kill any process with PID given in brackets

Electrolyze answered 3/10, 2015 at 19:44 Comment(0)
N
0

Try "kill -9 $proid" or "kill -SIGKILL $proid" commands. If you want more information, click.

Newsome answered 3/10, 2015 at 19:45 Comment(1)
SIGKILL should only be used as a last resort: it prevents the process from gracefully shutting down. For more details, see When should I not kill -9 a process?Asceticism
C
0

Based on what you have there, it looks like you aren't getting the actual PID in your proid variable. If you want to capture the output of pidof, you will need to enclose that command in backtics for the old form of command substitution ...

proid=`pidof $proceso`

... or like so for the new form of command substitution.

proid=$(pidof $proceso)
Culmination answered 3/10, 2015 at 19:57 Comment(1)
I realized that after you guys told me haha thanks a lot !Draff
M
0

I had a similar problem, only wanting to run monitor (Video surveillance) for several hours a day. Wrote two sh scripts;

cat startmotion.sh

#!/bin/sh
motion -c /home/username/.config/motion/motion.conf

And the second; cat killmotion.sh

#!/bin/sh
OA=$(cat /var/run/motion/motion.pid)
kill -9 $OA

These were called from crontab at the scheduled time

ctontab -e
0 15 * * * /home/username/startmotion.sh
0 17 * * * /home/username/killmotion.sh

Very simple, but that's all I needed.

Maliamalice answered 10/11, 2015 at 4:19 Comment(0)
B
0

On mac:

if port is 8080 for example:

run: lsof -i :8080

you'll get this:

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    12345 user   12u  IPv6 0t0    TCP *:http-alt (LISTEN)

run:

kill -9 12345

Bisque answered 13/6 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.