Running a Ruby Program as a Windows Service?
Check out the following library: Win32Utils. You can create a simple service that you can start/stop/restart at your leisure. I'm currently using it to manage a Mongrel instance for a Windows hosted Rails app and it works flawlessly.
Run a ruby exe file as windows service leads to error
You should have a look at this question.
Check out Win32Utils - Ruby library for MS Windows.
From the doc,
Note that some libraries, such as win32-api and win32-service, have
gems that contain prebuilt binaries so no compiler is necessary for
those libraries. Just install them as you would any other gem.
There are some nice wrappers for these Utils. win32-service is one such gem will help you to acheive that.
Go through these Create a Windows Service with Ruby Part 1 , Part 2 articles by Steve Johnson.
Run WEBrick as a Windows service
I have never seen WEBrick used in Windows services but a workaround could be running the web server as a background job. This is how you would do it:
You'll need a bat file with the 2 instructions to start the web server:
- cd to_your_absolute_app_path
- rails s
Then you'll need to convert that bat file to an exe file. Check this for a free tool. I know the tool exists for 32 and 64 bits, you might need to look around for the right one.
When you create the exe make sure to select it to run as an invisible app.
Then you put the exe in the start up folder. That should do it.
Ruby script as service
I've actually found a much better way of doing that by using ruby scripts.
This is how I did it:
First of all, I installed daemon
gem install daemons
Then I did:
require 'rubygems'
require 'daemons'
pwd = File.dirname(File.expand_path(__FILE__))
file = pwd + '/runner.rb'
Daemons.run_proc(
'my_project', # name of daemon
:log_output => true
) do
exec "ruby #{file}"
end
I then create a file called runner.rb, in which I can call my scripts such as:
require "/var/www/rails/my_project/config/environment"
Post.send('details....')
Daemons is a great gem!
how to install windows service using Chef Script
Try removing the service if it already exists with a powershell_script
block and notifies the execute
block to install/reinstall the service. This way the installation will only fire after the service is removed.
powershell_script 'delete_if_exist' do
code <<-EOH
$Service = Get-WmiObject -Class Win32_Service -Filter "Name='TestService'"
if ($Service) {
$Service.Delete()
}
EOH
notifies :run, 'execute[Installing Service TestService]', :immediately
end
execute 'Installing Service TestService' do
command "sc create \"TestService\" binPath= D:/Deploy/TestService.exe "
action :nothing
end
Can I use guards to chef if a windows service is running?
The way you wrote your not_if
statement runs the command as a shell script.
The shell doesn't know Ruby code, so the whole command will fail.
Need to first:
require win32/service
In order to use not_if
with Ruby code you should put it inside a block instead:
not_if { ::Win32::Service.exists?("Service name") }
See some more examples here (search for not_if
on the page):
https://docs.chef.io/resource_common.html
programmatically stop a windows service using ruby
The %x command, and Windows command line utilities like "AT" and "NET STOP".
e.g.
@result = %x[AT \\computername 00:00 "NET STOP service"]
00:00 being the current time plus a reasonable buffer to account for the clocks being out of sync. Also, the AT command requires elevation on Vista/Server 2008/Windows 7.
Related Topics
Rails "Undefined Method for Activerecord_Associations_Collectionproxy"
What Ide to Use for Developing in Ruby on Rails on Windows
Regex Match Everything Up to First Period
Redis or Mongo for Determining If a Number Falls Within Ranges
Overriding the == Operator in Ruby
Creating a Thread-Safe Temporary File Name
While Executing Gem, Unknown Command
Preload Has_Many Associations with Dynamic Conditions
In Ruby, How to Output JSON from Hash and Give It Line Breaks and Tabs
Ruby - Dynamically Add Property to Class (At Runtime)
Can't Get Rack-Cors Working in Rails Application
One Liner in Ruby for Displaying a Prompt, Getting Input, and Assigning to a Variable
What Is the Best/How to Validate an Email Address in Ruby