Joel Flores Sottile
Joel Flores Sottile

Reputation: 411

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

Upvotes: 33

Views: 261427

Answers (9)

Itay Tur
Itay Tur

Reputation: 1737

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

Upvotes: 3

Tadele Ayelegn
Tadele Ayelegn

Reputation: 4746

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

Upvotes: 55

Wally
Wally

Reputation: 1

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.

Upvotes: 0

janos
janos

Reputation: 124804

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.

Upvotes: 9

wyrm
wyrm

Reputation: 317

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)

Upvotes: 0

Shawn Mehan
Shawn Mehan

Reputation: 4578

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.

Upvotes: 4

user5152421
user5152421

Reputation:

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

Upvotes: 0

jain28
jain28

Reputation: 137

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

Upvotes: 0

shafeen
shafeen

Reputation: 2477

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.

Upvotes: 0

Related Questions