Reputation: 411
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
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
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
Upvotes: 55
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
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
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
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:
-9
) signal forces the process to stop executing immediately. The program cannot ignore this signal. This process does not get to clean-up either.Upvotes: 4
Reputation:
Try "kill -9 $proid" or "kill -SIGKILL $proid" commands. If you want more information, click.
Upvotes: 0
Reputation: 137
Try this kill -9 It will kill any process with PID given in brackets
Upvotes: 0
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