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?

2
  • 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, 2015 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. Apr 12, 2015 at 15:32

3 Answers 3

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.

5
  • I see. Thanks so much for the input. What about the tkill() and tgkill() commands?
    – Daren Tan
    Apr 12, 2015 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, stackoverflow.com/a/27000635/2072269
    – muru
    Apr 12, 2015 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, 2018 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, 2018 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 1, 2018 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.

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

3
  • 1
    As I read so far, kill is for killing the entire process right? But in my case, I only want to kill a single thread inside of a process with multiple threads.
    – Daren Tan
    Apr 12, 2015 at 13:18
  • That's really about killing the process altogether. It has nothing to do with threads. Dec 13, 2017 at 10:25
  • This kills the process he is asking to kill a single thread of a process. Jan 11, 2019 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.