how to configure a rails app (redmine) to run as a service on windows?

后端 未结 7 799
你的背包
你的背包 2020-12-07 23:28

I\'m using redmine as a ticket manager, and I\'d like to configure it to be run automatically when windows starts up.

How can I configure it to be run as a service?<

7条回答
  •  旧时难觅i
    2020-12-08 00:14

    • 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:\Ruby23-x64\bin\rubyw -C E:\www\redmine-3.3.2\ service.rb" where E:\www\redmine-3.3.2\ is the path of the directory where the service.rb file is located and C:\Ruby23-x64\bin\rubyw your Ruby installation path

    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']
          # use full path
          @server_pid = Process.spawn 'C:\Ruby23-x64\bin\ruby E:\www\redmine-3.3.2\bin\rails s -e production -p 3000', :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
    
    • Note that Process.spawn in the service.rb use the full path.

提交回复
热议问题