184

I am trying to improve my command line skills and I have encountered a problem where I cannot kill a process. I type kill 2200 where 2200 is my PID and the process is not killed. After few minutes wait is still in the top and ps aux. I have even tried typing it with sudo - no results.

Any ideas why it would be like that ?


EDIT

I have found a weird dependency, where fg updates the processes list:

x@xxx:/etc/grub.d$ ps
  PID TTY          TIME CMD
 1723 pts/0    00:00:00 bash
 2200 pts/0    00:00:00 top
 2202 pts/0    00:00:00 top
 2258 pts/0    00:00:00 ps
x@xxx:/etc/grub.d$ fg
top

x@xxx:/etc/grub.d$ ps
  PID TTY          TIME CMD
 1723 pts/0    00:00:00 bash
 2200 pts/0    00:00:00 top
 2620 pts/0    00:00:00 ps
x@xxx:/etc/grub.d$ fg
top

x@xxx:/etc/grub.d$ ps
  PID TTY          TIME CMD
 1723 pts/0    00:00:00 bash
 2621 pts/0    00:00:00 ps
muru
  • 197,895
  • 55
  • 485
  • 740
Patryk
  • 9,146
  • What process was that? Did you check if the process maybe was defunct? In that case you'd need to kill the parent process. – htorque Sep 03 '11 at 07:49
  • The process is top (as listed in the edit). I just wanted to try putting program to work into background and then bringing it back. – Patryk Sep 03 '11 at 07:54
  • 2
    If you suspend a process with CTRL-z, it'll block most signals as long as it's suspended (i.e. until you do a fg or bg for the process) – nos Jun 10 '14 at 08:09

8 Answers8

280

Processes can ignore some signals. If you send SIGKILL it will not be able to ignore it (and neither catch it to do cleanups). Try:

kill -9 {PID}

Learn more by reading the manual page:

man kill
Lekensteyn
  • 174,277
  • 33
    also note that in some very specific circumstances, a process can be in a zombie/defunct state that even SIGKILL cannot kill the process. In that case, you will have to find the parent process and kill the parent process. – Lie Ryan Sep 03 '11 at 11:49
  • 25
    If that process steps out of line then it's KILL DASH NINE! – scottl Sep 05 '11 at 04:22
  • 7
    And sometimes there is no parent process, in which case you're just screwed. The only way to remove such a process is to reboot the machine. – user606723 Sep 06 '11 at 15:25
  • You can also just use pkill process, where process is the name of the process in stead of the process ID. – RobinJ Sep 06 '11 at 17:26
  • 3
    The name of the kill command continues to be misleading for many, many users (including me at the beginning). One assumes that when you say "kill X" this means really kill the X and not do something else. I understand this won't change a thing but I wish they have picked a more elaborative name... – rbaleksandar Jul 17 '15 at 14:08
  • 7
    What do even after kill -9 doesn't work and the process is still hanging around? – Douglas Gaskell Aug 08 '18 at 05:44
  • is there a reason why "kill # 9" is the one that works? (i.e. so I can better remember kill -9 is what works and not rote memorize it) – user391339 Jan 27 '19 at 07:18
  • @user391339 every signal has it's number (see: https://en.wikipedia.org/wiki/Signal_(IPC)#POSIX_signals ) It was set arbitrarily almost 50 years ago on UNIX systems. Some signals as so important that they are consistent across different UNIX systems. – Michał Šrajer Jan 28 '19 at 13:26
  • sweet and awesome – Rafiq May 18 '21 at 20:51
  • man kill says almost nothing about -9: "Alternate signals may be specified in three ways: -9, -SIGKILL or -KILL." I would not understand that means kill. Could one use -KILL instead of -9, e.g. kill -KILL {PID}? – run_the_race Mar 17 '22 at 19:13
73

If kill is invoked without any parameter, it sends the signal number 15 (SIGTERM). This signal can be ignored by the process. This signal notifies the process to clean his things up and then end correctly by himself. That's the nice way.

You can also "send" the signal number 9 (SIGKILL) that cannot be ignored by the process. The process will even not recognize it, because the kernel ends the process, not the process itself. That's the evil way.

One says kill -9 <pid> always works. That's a misbelief. There are situations where even kill -9 does not kill the process. For example when a process has the state D (uninterruptable sleep). A process comes into this state everytime it waits for I/O (normally not very long). So, if a process waits for I/O (on a defect harddisk for example) and it is not programmed properly (with a timeout), then you simply cannot kill the process. No matter what you do. You just can try to make the file accessible that the process continues.

chaos
  • 27,506
  • 12
  • 74
  • 77
  • 5
    This is very helpful, I have experienced this several times because of hanging I/O access on network disks and I was wondering why I couldn't kill processes that froze. Is there more documentation on this specific issue and how to get around it? – Jonathan H Jul 06 '14 at 20:13
  • I had this happen with /usr/bin/kate -b sftp://foo/root/bar.txt and it wouldn't die. After killing the parent it was just inherited by PID 1. I even tried using gdb to close the FD but it can't attach because the process is hung. – Sarke Oct 06 '22 at 06:23
  • Thanks! I had to close VSCode so that the file becomes available – Mendy Mar 15 '23 at 19:42
9

Despite it's name kill doesn't actually kill processes, it sends signals to it. From the man page:

kill - send a signal to a process

The default signal sent by kill [pid] is SIGTERM which usually but not necessarily asks the process to terminate. It's quite possible to write a program that plays a happy tune when you send the SIGTERM signal to it, but not recommended.

Another common signal is SIGHUP which is often used to ask a program to reread its configuration files.

If you really want to kill a program you need to use the SIGKILL signal by doing kill -9 [pid].

danne
  • 512
2

This is what i used to pill localhost running on port 80 (By angular cli) Get running app info on port 80

sudo lsof -i tcp:80

After That 
sudo kill -9 3348

where 3348 is pid of the running process

Aklesh Singh
  • 121
  • 2
2

It sounds like you might be suspending a process (perhaps by pressing Ctrl-Z in the terminal). In this state, your process will not respond to a SIGTERM as it is frozen. Running 'fg' thaws the process, so it can pick up the signal and self-terminate. That could explain why 'fg' appears to update the process list.

0

You also may use kill -l to display the supported signals by your architecture, and learn more about the signal you may wish to use to properly send a signal.

Note: as others may have mentioned, the use of kill -9 {PID} is not recommended unless its a zombie process. once a process receives a SIGKILL will shutdown immediately without cleaning or any other proper procedures.

amrx
  • 1,360
0

If you trying to kill a process with PID and it still runs on another PID, it looks like you have started that process in a different account most probably root accout. so Login in with sudo su and kill it

0

From within C++, I executed:

kill(4024, SIGKILL);

And on a linux(Ubuntu) terminal,

$ ps -ax | grep my_su

The output was:

4024 pts/1    Z+     0:00 [my_subscriber] <defunct>

Seemingly, it (4024) still surviving. However, as soon as I terminated the parent process which called the above "kill" statement, 4024 didn't appear any more. Now I judge "defunct" process is nothing more than a line displayed and decided to ignore it. I hope my experience could help someone out there. Cheers!

Park JongBum
  • 131
  • 3