11

I'm new to Ubuntu and new here. I need to know this.

How do I kill a single thread from Terminal in Ubuntu? I have mozilla opened and it has 45 threads. I want to kill one of it.

I've already search many sources online but to no avail. Can anyone help me?

Daren Tan
  • 111
  • 1
  • 1
  • 3
  • You might want to have a look at the manpages of `pgrep` and `pkill`. Type `man pgrep` or `man pkill` into your terminal. Exit the manpage with pressing `q`. – Byte Commander Apr 12 '15 at 12:18
  • 2
    What do you expect to happen if you were able to kill one thread of an application? Multithreaded programs work as a whole, and unexpected termination of one of its threads would almost always lead to undefined results, usually crashing the application. Also, do not worry it a program uses that many threads, it's perfectly fine - and the number of threads is not strictly correlated with resource usage. So unless many of these threads perform costly operations, your system should be just fine with them. – Rafał Cieślak Apr 12 '15 at 15:32

3 Answers3

7

You can't. Not without killing the whole process. From man 3 pthread_kill, a function used for signal handling:

NOTES
   Signal dispositions are process-wide: if a signal handler is installed,
   the  handler  will  be  invoked  in  the  thread  thread,  but  if  the
   disposition  of  the signal is "stop", "continue", or "terminate", this
   action will affect the whole process.

Also see this U&L post.

muru
  • 193,181
  • 53
  • 473
  • 722
  • I see. Thanks so much for the input. What about the tkill() and tgkill() commands? – Daren Tan Apr 12 '15 at 13:16
  • @DarenTan those are not commands, but functions to be used in code. In any case, stopping, continuing or terminating affects the entire process, irrespective of the function used. See, for example, http://stackoverflow.com/a/27000635/2072269 – muru Apr 12 '15 at 13:20
  • Ok, but there should be a way to send another signal to just one thread right ? In my case I am looking for that in order to launch the debugger in the thread to see what is doing. I guess tgkill() needs a command line interface. – yucer Feb 15 '18 at 14:33
  • @yucer if it's a signal other than those three mentioned above, and if that thread has a handler installed for it, sure. – muru Feb 15 '18 at 14:45
  • The kill commands support all the ones shown by "kill -l". Are all of them related to "stop", "continue" or "terminate" ? I am looking for one to tell a particular thread to dumps its stack trace in order to see what is it doing. And yes, the thread is already ready to handle a signal. – yucer Mar 01 '18 at 17:08
0

This seems to be what tgkill is for, but it's unlikely the best way to deal with too many threads opened by application. It should be treated as a sort of debugging tool.

Vi.
  • 220
  • 1
  • 6
-1

In terminal type:

kill -9 "pid number for the process"

For example:

kill -9 5624 (number 9 is the code for kill signal)

To get the PID number for the process either use:

ps -eLF

or you could use the command:

top

Every process will have it's own PID number. The reason for this is because processes can be named the same.

Lars Martinsen
  • 342
  • 3
  • 7