How can I monitor the thread count of a process on linux?

前端 未结 17 915
暖寄归人
暖寄归人 2020-12-02 03:53

I would like to monitor the number of threads used by a specific process on Linux. Is there an easy way to get this information without impacting the performance of the proc

相关标签:
17条回答
  • 2020-12-02 04:25

    Here is one command that displays the number of threads of a given process :

    ps -L -o pid= -p <pid> | wc -l
    

    Unlike the other ps based answers, there is here no need to substract 1 from its output as there is no ps header line thanks to the -o pid=option.

    0 讨论(0)
  • 2020-12-02 04:30

    ps -eLf on the shell shall give you a list of all the threads and processes currently running on the system. Or, you can run top command then hit 'H' to toggle thread listings.

    0 讨论(0)
  • 2020-12-02 04:34

    If you are trying to find out the number of threads using cpu for a given pid I would use:

    top -bc -H -n2 -p <pid> | awk '{if ($9 != "0.0" && $1 ~ /^[0-9]+$/) print $1 }' | sort -u | wc -l
    
    0 讨论(0)
  • 2020-12-02 04:35

    try

    ps huH p <PID_OF_U_PROCESS> | wc -l
    

    or htop

    0 讨论(0)
  • 2020-12-02 04:36

    The easiest way is using "htop". You can install "htop" (a fancier version of top) which will show you all your cores, process and memory usage.

    Press "Shift+H" to show all process or press again to hide it. Press "F4" key to search your process name.

    Installing on Ubuntu or Debian:

    sudo apt-get install htop
    

    Installing on Redhat or CentOS:

    yum install htop
    dnf install htop      [On Fedora 22+ releases]
    

    If you want to compile "htop" from source code, you will find it here.

    0 讨论(0)
  • 2020-12-02 04:37

    JStack is quite inexpensive - one option would be to pipe the output through grep to find active threads and then pipe through wc -l.

    More graphically is JConsole, which displays the thread count for a given process.

    0 讨论(0)
提交回复
热议问题