Project

General

Profile

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

Etienne Massip, 2012-06-14 19:02
Initial version

1 1 Etienne Massip
h1. HowTo run Redmine as a Windows service (@win32-service@ + @taskill@ approach)
2
3
* @gem install win32-service@
4
* drop below Ruby code in a @service.rb@ file and update @REDMINE_DIR@ path to fit your Redmine installation
5
* 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
6
7
<pre><code class="ruby">
8
REDMINE_DIR = 'D:\redmine'
9
LOG_FILE = "#{REDMINE_DIR}\\log\\service.log"
10
11
begin
12
  require 'win32/daemon'
13
  include Win32
14
15
  class RedmineService < Daemon
16
17
    def service_init
18
      File.open(LOG_FILE, 'a'){ |f| f.puts "Initializing service #{Time.now}" } 
19
20
      @server_pid = Process.spawn 'ruby script/rails s -e production', :chdir => REDMINE_DIR, :err => [LOG_FILE, 'a']
21
    end
22
23
    def service_main
24
      File.open(LOG_FILE, 'a'){ |f| f.puts "Service is running #{Time.now} with pid #{@server_pid}" }
25
      while running?
26
        sleep 10
27
      end
28
    end
29
30
    def service_stop
31
      File.open(LOG_FILE, 'a'){ |f| f.puts "Stopping server thread #{Time.now}" }
32
      system "taskkill /PID #{@server_pid} /T /F"
33
      Process.waitall
34
      File.open(LOG_FILE, 'a'){ |f| f.puts "Service stopped #{Time.now}" }
35
      exit!
36
    end
37
  end
38
39
  RedmineService.mainloop
40
41
rescue Exception => e
42
  File.open(LOG_FILE,'a+'){ |f| f.puts " ***Daemon failure #{Time.now} exception=#{e.inspect}\n#{e.backtrace.join($/)}" }
43
  raise
44
end
45
</code></pre>