Monday, July 6, 2009

A neat way to monitor windows services

The JDK supplies a very impressive toolchest which enables you to monitor the JVM(
jconsole, jps, jmap,jvisualvm etc...). There are also other tools such as BTrace which can attach to the JVM and solve performance problems.
On UNIX you only need to use su or sudo and act as the user running the JVM. Doing that you can attach to it and apply all commands.
In Microsoft Windows the situation is more complicated. The service processes run under a different user and in another session and you are not able to connect to them.

But do not panic:-) I am suggesting here a simple workaround.

Step 1:
Download and install the Resource Kit Tools from Microsoft. (The 2003 version will also run on Windows XP). Didn't try Vista/2008 but then again who uses Vista.

Step 2:
Run the following command:
%RESKIT_HOME%\Tools\Instsrv.exe" SrvCMD "%RESKIT_HOME%\Tools\Srvany.exe"
It will create a service named SrvCMD (in case you have such a service use a different name)

Step 3:
Open services.msc and update the service properties to run manually. Check "allow service interact with desktop" box.

Execute regedit.
Open the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SrvCMD
Add a sub-key named Parameters
To this sub-key add a string value named Application. Its value will be the name of the executable you want to run. In our case c:\WINDOWS\system32\cmd.exe.

Note: If you are connecting with remote desktop make sure you are connecting with /console parameter (otherwise you will not see the command window)
Start the service.
A window with command prompt running in the session of the service will be opened.
jps will now discover your JVM.



Moshe Ben Shoham said...

Very useful!!!

Moshe Ben Shoham said...

Update for Windows 7: A simple command line window will not allow you to run Instsrv.exe because it does not open with enough privileges. To open a command line window as administrator (provided that you are logged in as administrator), go to the "run" box in the start menu, type in "cmd" but instead of clicking Enter, click Ctrl + Shift + Enter. You'll get a pop up asking you to confirm you want to allow cmd make changes to your computer. Approve and you're good to go.