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

1 createservice

Check it exists

2 service

Set Parameters In The Registry

Configure it at HKLM/SYSTEM/CurrentControlSet/Services/APPNAME/Parameters

Screen shot 2011 02 14 at 12.54.15

Windows Registry Editor Version 5.00

"AppParameters"="C:\\Sites\\vmdeploy\\server.rb -p 80"

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.