Tuesday, February 8, 2011

Is there any way, in java, to check on the status of a windows service?

I am looking for a library that will allow me to look up the status of a windows service to verify that the service is started and running. I looked into the Sigar library, but it is GPL and therefor I cannot use it. A Commercial or BSD(ish) license is required as this will be bundled into commercial software.

  • I don't know of any libraries, but depending on how detailed you need to get you might get by with some shell commands and parsing the output.

    NET START servicename

    will either start the service, or give you back an error message that tells you its already started. I don't know of any command that will just give you the status though.

  • I don't think there is any pure-Java way to do this because some operating systems don't have the notion of "services" like Windows does. In our projects, we wrote a wrapper around calls to the "sc" command from the command line. To get the status of a service, you can do:

    sc \\some-computer query "my service name"

    You'll have to manually parse the output but it's pretty straightforward.

    Jan Gressmann : good simple way to do this in Java
  • If nothing else helps, try to think of a slightly different approach (if you can, of course), e.g.:

    • There is a plenty of free/non-free software which does monitoring, including Windows service monitoring (e.g. nagios, Zabbix, etc.). These monitors typically have open API where your Java app could integrate into in a number of different ways.
    • If you have the control over depending service application, expose another, different way for your Java application to check (e.g. run a dummy listener on a port, create a file, etc.). Windows services aren't a cross-platform thing therefore is not something you would expect to be supported anytime soon.
    From mindas

0 comments:

Post a Comment