2008-05-28

Windows Service using SrvAny

Microsoft have a good page on debugging Troubleshooting SrvAny using Cmd.exe which I found helpful.
Here is someone who has done the same thing for WEBrick as a Windows Service. (ps the old method of installing parameters seems to work for srvany on XP).
You can write a very small program and make it run.

File.open("c:\\simpleDaemon.log", "a"){ |f| f.puts "Service started #{Time.now}" }
#while true #Would normally use an endless while loop so that service would not finish
(1..3).each {|i|
sleep 3
File.open("c:\\simpleDaemon.log", "a"){ |f| f.puts "Service ran at #{Time.now}" }
}
File.open("c:\\simpleDaemon.log", "a"){ |f| f.puts "Service closed #{Time.now}" }

This can be run happily from the commandline or NetBeans as normal.
So now you can create a service using the command line;
c:>sc create test1 binpath= "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"
or
c:>"\Program Files\Windows Resource Kits\Tools\instsrv" test1 "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"
instsrv results
Now you have to arrange the parameters using Regedit to get the program to run.
However using SrvAny has a number of problems. If the program stops working then you have no way of knowing what the problems was due to. Also the service itself still appears to be running. The solution to this is to use the Daemon class from win32.

No comments: