HowTo run Redmine as a Windows service (win32-service + taskill approach) » History » Version 2

« Previous - Version 2/3 (diff) - Next » - Current version
Etienne Massip, 2012-06-14 23:36
Added link to sc command reference.


HowTo run Redmine as a Windows service (win32-service + taskill approach)

  • gem install win32-service
  • drop below Ruby code in a service.rb file and update REDMINE_DIR path to fit your Redmine installation
  • create the service, for example with sc create redmine binPath="C:\Ruby193\bin\rubyw -C D:\redmine\ service.rb" where D:\redmine\ is the path of the directory where the service.rb file is located and C:\Ruby193 your Ruby installation path
REDMINE_DIR = 'D:\redmine'
LOG_FILE = "#{REDMINE_DIR}\\log\\service.log" 

begin
  require 'win32/daemon'
  include Win32

  class RedmineService < Daemon

    def service_init
      File.open(LOG_FILE, 'a'){ |f| f.puts "Initializing service #{Time.now}" } 

      @server_pid = Process.spawn 'ruby script/rails s -e production', :chdir => REDMINE_DIR, :err => [LOG_FILE, 'a']
    end

    def service_main
      File.open(LOG_FILE, 'a'){ |f| f.puts "Service is running #{Time.now} with pid #{@server_pid}" }
      while running?
        sleep 10
      end
    end

    def service_stop
      File.open(LOG_FILE, 'a'){ |f| f.puts "Stopping server thread #{Time.now}" }
      system "taskkill /PID #{@server_pid} /T /F" 
      Process.waitall
      File.open(LOG_FILE, 'a'){ |f| f.puts "Service stopped #{Time.now}" }
      exit!
    end
  end

  RedmineService.mainloop

rescue Exception => e
  File.open(LOG_FILE,'a+'){ |f| f.puts " ***Daemon failure #{Time.now} exception=#{e.inspect}\n#{e.backtrace.join($/)}" }
  raise
end