javabashthread-dump

Is there a simple way to generate a jstack for each running jvm?


I would like to go like this

jstack ALL

or something like that so i can get the thread dump for all jvms on my system.

Can it be done?


Solution

  • The jstack command reference offers these three invocation forms:

    jstack [ options ] pid
    
    jstack [ options ] executable core
    
    jstack [ options ] [ server-id@ ] remote-hostname-or-IP
    

    Of those, only the first is relevant to JVMs currently running on the same system where jstack runs. Thus, you must identify the JVM processes by their pids.

    In its summary of the pid argument, the reference notes:

    To get a list of Java processes running on a machine, use the jps(1) command.

    Presumably, if you have jstack available to you then you also have jps. The docs suggest that you might use jps -q to get a listing of only the relevant pids, that you therefore do not need to massage. Given that, you could do something like this in bash:

    for vmpid in $(jps -q); do
      jstack $vmpid
    done
    

    Evidently jps is implemented in Java, for I find that it reports on itself. If that bothers you then the above could be tweaked to filter out jps itself.