Skip to content

Instantly share code, notes, and snippets.

@tachesimazzoca
Created August 21, 2018 02:41
Show Gist options
  • Save tachesimazzoca/01a366026b6eb5c3341a13fc55c35a15 to your computer and use it in GitHub Desktop.
Save tachesimazzoca/01a366026b6eb5c3341a13fc55c35a15 to your computer and use it in GitHub Desktop.
Diagnostic Tools in Java

Diagnostic Tools in Java

jcmd

# The following error occurs when the pid owned by the diffrent user.
$ jcmd <pid> help
<pid>:
com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded
        at sun.tools.attach.LinuxVirtualMachine.<init>(LinuxVirtualMachine.java:106)
        at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:63)
        at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:213)
        at sun.tools.jcmd.JCmd.executeCommandForPid(JCmd.java:140)
        at sun.tools.jcmd.JCmd.main(JCmd.java:129)

# You need to do as the same user, but the followning error might occured due to the /etc/sudoers settings.
$ sudo -u tomcat jcmd <pid> help
Sorry, user <username> is not allowed to execute '/usr/bin/jcmd <pid> help' as tomcat on <hostname>.

# if you're allowed to use the `su`, switch to the pid owner and then issue the `jcmd`.
# You may also need to add the shell option (-s) if that user uses /sbin/nologin.
$ sudo su -s /bin/sh - tomcat -c "jcmd <pid> help"
<pid>:
The following commands are available:
VM.native_memory
GC.rotate_log
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
Thread.print
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help

For more information about a specific command use 'help <command>'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment