Running Any Executable As A Windows Service (Ruby / Sinatra)

While migrating an automated VM deployment page using a combination of Sinatra on Linux and Bash scripts using the Perl toolkit with a simpler script using the VMWare PowerCLI that I love so much I needed to create a windows service from the Sinatra App and had to do some googleing so I thought I would share how I did it.

You only need two things – the built-in “sc” command and an executable from Windows Server 2003 Resource Kit Tools called srvany (works with 2008 too). Get just that exe here (if you trust me of course šŸ˜‰ )

Creating the service

Check it exists

Set Parameters In The Registry
Configure it at HKLM/SYSTEM/CurrentControlSet/Services/APPNAME/Parameters

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesVMdeployParameters]
"Application"="C:\Ruby192\bin\ruby"
"AppParameters"="C:\Sites\vmdeploy\server.rb -p 80"
"AppDirectory"="C:\Sites\vmdeploy"
"AppEnvironment"=hex(7):65,00,78,00,61,00,6d,00,70,00,6c,00,65,00,3d,00,32,00,
  37,00,00,00,62,00,6c,00,61,00,68,00,3d,00,63,00,3a,00,5c,00,74,00,65,00,6d,
  00,70,00,66,00,69,00,6c,00,65,00,73,00,00,00,00,00

Note the AppEnvironment is a multiline string, the rest are strings

This lets you run any executable file, change the directory you run it from and pass any arguments or environment variables so should cover most use cases.

I will be sharing the code for both the Sinatra app and the PowerShell deploy script in later posts.

2 thoughts on “Running Any Executable As A Windows Service (Ruby / Sinatra)

  1. Luis Lavena February 14, 2011 at 2:03 pm Reply

    Hello,

    Thank you for sharing!

    One thing to mention about srvany.exe is that it kills the Ruby process when “net stop” is invoked, it doesn’t terminate it properly (like sending Ctrl+C or Ctrl+Pause)

    This could lead to broken files or lost queries in case of requests been processed.

    There are other tools in the market, but sadly, not free.

    I’ve suggested someone port the work done with mongrel_service to use thin, so it can host any rack based application.

    Cheers.

  2. tom February 17, 2011 at 2:17 pm Reply

    @Luis, thanks for the comment. Our app is insanely simple so I am not too worried but did not actually know that so will have to keep it in mind in future.

Leave a comment