代码之家  ›  专栏  ›  技术社区  ›  opensas

如何将Rails应用程序(Redmine)配置为在Windows上作为服务运行?

  •  16
  • opensas  · 技术社区  · 16 年前

    我使用Redmine作为票务管理器,我想将其配置为在Windows启动时自动运行。

    如何将其配置为作为服务运行?

    ——

    只是问这个问题来记录它,我希望有人能发现它有用…

    7 回复  |  直到 6 年前
        1
  •  29
  •   opensas    8 年前

    1。使用韦布里克:

    裁判: http://www.redmine.org/boards/1/topics/4123

    • 从下载并安装Windows NT资源工具包 http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en

    • 通过运行以下命令创建服务:

      path\INSTSRV.EXE your_service_name path\SRVANY.EXE
      

      以我为例 path 是:

      "C:\Program Files\Windows NT Resource Kit\INSTSRV.EXE" redmine_webrick "C:\Program Files\Windows NT Resource Kit\SRVANY.EXE"
      

      也可以 C:\Program Files\Windows Resource Kits\Tools\ .

    • 运行regedit(开始->运行->regedit)

      • 添加以下注册表项(如果尚未存在):

        HKEY U本地机器\系统\当前控制集\服务\您的服务名称

      • 右键单击此注册表项并选择“新建”->项。说出它的名字 Parameters .

      • 将两个值添加到 参数 关键。右键单击参数键,新建->字符串值。说出它的名字 Application . 现在创建另一个名为 AppParameters . 给他们下列值:

        • 应用: PathToRuby.exe ,例如 C:\ruby\bin\Ruby.exe
        • AppParameters: C:\RUBYAPP\script\server -e production 在哪里 RUBYAPP 是包含Redmine网站的目录。

        例子: C:\redmine\script\server -p 2000 -e production (-p表示Webrick将要监听的端口,以及-e所使用的环境)

    现在,您可以转到“管理工具”->服务。在那里你可以开始你的服务 your_service_name )测试它是否正常工作。应该注意,在Webrick完成引导过程之前,服务将被标记为已启动。在尝试访问服务以验证它是否正常工作之前,应该给它1分钟左右的时间。

    2。使用杂种:

    裁判: http://mongrel.rubyforge.org/wiki 裁判: http://mongrel.rubyforge.org/wiki/Win32

    首先安装Mongrel和Mongrel_Service Gem

    gem install mongrel
    
    gem install mongrel_service
    

    然后创建服务

    mongrel_rails service::install -N redmine_mongrel -c c:\redmine -p 3000 -e production
    

    三。使用薄:

    参考文献:

    说明:

    1. 首先安装thin(如果尚未安装,则需要安装rack gem 安装)

      gem install rack     
      gem install thin
      
    2. 按照与Webrick相同的步骤操作,但添加另一个名为“appdirectory”的值。为了避免使用c:\ruby\bin\thin.bat,需要这样做。如果我只是指向BAT文件,我就无法停止服务。

      HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\redmine_thin\Parameters 添加以下键:

      应用程序:c:\ruby\bin\ruby.exe

      应用程序目录:C:\Redmine

      应用程序参数:c:\ruby\bin\thin start-p 4000-e production

    ——————————————————————————————————————————————————————————————————————————————————————————————————————————————————

    您可以使用以下命令控制任何服务:

    网络启动Redmine_xxx

    净停止红矿

    sc config redmine_xxx start=自动

    sc config redmine_xxx start=auto dependency=mysql

    SC删除Redmine_xxx

        2
  •  4
  •   Bohdan    13 年前

    对于Rails 3.0.x应用程序(在3.0.10和Windows 7上测试)

    演示守护进程ctl.rb

    ############################################################################
    # demo_daemon_ctl.rb
    #
    # This is a command line script for installing and/or running a small
    # Ruby program as a service.  The service will simply write a small bit
    # of text to a file every 20 seconds. It will also write some text to the
    # file during the initialization (service_init) step.
    #
    # It should take about 10 seconds to start, which is intentional - it's a test
    # of the service_init hook, so don't be surprised if you see "one moment,
    # start pending" about 10 times on the command line.
    #
    # The file in question is C:\test.log.  Feel free to delete it when finished.
    #
    # To run the service, you must install it first.
    #
    # Usage: ruby demo_daemon_ctl.rb <option>
    #
    # Note that you *must* pass this program an option
    #
    # Options:
    #    install    - Installs the service.  The service name is "DemoSvc"
    #                 and the display name is "Demo".
    #    start      - Starts the service.  Make sure you stop it at some point or
    #                 you will eventually fill up your filesystem!.
    #    stop       - Stops the service.
    #    pause      - Pauses the service.
    #    resume     - Resumes the service.
    #    uninstall  - Uninstalls the service.
    #    delete     - Same as uninstall.
    #
    # You can also used the Windows Services GUI to start and stop the service.
    #
    # To get to the Windows Services GUI just follow:
    #    Start -> Control Panel -> Administrative Tools -> Services
    ############################################################################
    require 'win32/service'
    require 'rbconfig'
    include Win32
    include Config
    
    # Make sure you're using the version you think you're using.
    puts 'VERSION: ' + Service::VERSION
    
    SERVICE_NAME = 'DemoSvc'
    SERVICE_DISPLAYNAME = 'Demo'
    
    # Quote the full path to deal with possible spaces in the path name.
    ruby = File.join(CONFIG['bindir'], 'ruby').tr('/', '\\')
    path = ' "' + File.dirname(File.expand_path($0)).tr('/', '\\')
    path += '\demo_daemon.rb"'
    cmd = ruby + path
    
    # You must provide at least one argument.
    raise ArgumentError, 'No argument provided' unless ARGV[0]
    
    case ARGV[0].downcase
         when 'install'
                Service.new(
                     :service_name     => SERVICE_NAME,
                     :display_name     => SERVICE_DISPLAYNAME,
                     :description      => 'Sample Ruby service',
                     :binary_path_name => cmd
                )
                puts 'Service ' + SERVICE_NAME + ' installed'      
         when 'start' 
                if Service.status(SERVICE_NAME).current_state != 'running'
                     Service.start(SERVICE_NAME, nil, 'hello', 'world')
                     while Service.status(SERVICE_NAME).current_state != 'running'
                            puts 'One moment...' + Service.status(SERVICE_NAME).current_state
                            sleep 1
                     end
                     puts 'Service ' + SERVICE_NAME + ' started'
                else
                     puts 'Already running'
                end
         when 'stop'
                if Service.status(SERVICE_NAME).current_state != 'stopped'
                     Service.stop(SERVICE_NAME)
                     while Service.status(SERVICE_NAME).current_state != 'stopped'
                            puts 'One moment...' + Service.status(SERVICE_NAME).current_state
                            sleep 1
                     end
                     puts 'Service ' + SERVICE_NAME + ' stopped'
                else
                     puts 'Already stopped'
                end
         when 'uninstall', 'delete'
                if Service.status(SERVICE_NAME).current_state != 'stopped'
                     Service.stop(SERVICE_NAME)
                end
                while Service.status(SERVICE_NAME).current_state != 'stopped'
                     puts 'One moment...' + Service.status(SERVICE_NAME).current_state
                     sleep 1
                end
                Service.delete(SERVICE_NAME)
                puts 'Service ' + SERVICE_NAME + ' deleted'
         when 'pause'
                if Service.status(SERVICE_NAME).current_state != 'paused'
                     Service.pause(SERVICE_NAME)
                     while Service.status(SERVICE_NAME).current_state != 'paused'
                            puts 'One moment...' + Service.status(SERVICE_NAME).current_state
                            sleep 1
                     end
                     puts 'Service ' + SERVICE_NAME + ' paused'
                else
                     puts 'Already paused'
                end
         when 'resume'
                if Service.status(SERVICE_NAME).current_state != 'running'
                     Service.resume(SERVICE_NAME)
                     while Service.status(SERVICE_NAME).current_state != 'running'
                            puts 'One moment...' + Service.status(SERVICE_NAME).current_state
                            sleep 1
                     end
                     puts 'Service ' + SERVICE_NAME + ' resumed'
                else
                     puts 'Already running'
                end
         else
                raise ArgumentError, 'unknown option: ' + ARGV[0]
    end
    

    DimoO'Daimon RB

    APP_ROOT_CUSTOM = 'your app root dir'
    LOG_FILE = APP_ROOT_CUSTOM + 'log/win32_daemon_test.log'
    APP_PATH = File.expand_path( APP_ROOT_CUSTOM  + 'config/application', APP_ROOT_CUSTOM  + 'script/rails')
    
    begin  
        require 'rubygems'
        require 'win32/daemon'
        include Win32
        require File.expand_path( APP_ROOT_CUSTOM  + 'config/boot', APP_ROOT_CUSTOM  + 'script/rails')
        require 'rails/commands/server'
        module ::Rails
            class Server
                def default_options
                    super.merge({
                        :Port        => 3000,
                        :environment => (ENV['RAILS_ENV'] || "development").dup,
                        :daemonize   => false,
                        :debugger    => false,
                        :pid         => File.expand_path( APP_ROOT_CUSTOM + "tmp/pids/server.pid" ),
                        :config      => File.expand_path( APP_ROOT_CUSTOM + "config.ru" )
                    })
                end
            end
        end
    
        class DemoDaemon < Daemon       
            # This method fires off before the +service_main+ mainloop is entered.
            # Any pre-setup code you need to run before your service's mainloop
            # starts should be put here. Otherwise the service might fail with a
            # timeout error when you try to start it.
            #
            def service_init
            end
    
            # This is the daemon's mainloop. In other words, whatever runs here
            # is the code that runs while your service is running. Note that the
            # loop is not implicit.
            #
            # You must setup a loop as I've done here with the 'while running?'
            # code, or setup your own loop. Otherwise your service will exit and
            # won't be especially useful.
            #
            # In this particular case, I've setup a loop to append a short message
            # and timestamp to a file on your C: drive every 20 seconds. Be sure
            # to stop the service when you're done!
            #
            def service_main(*args)
    
                Rails::Server.new.tap { |server|
                    require APP_PATH
                    Dir.chdir( APP_ROOT_CUSTOM )
                    server.start
                }
    
                msg = 'application started at: ' + Time.now.to_s
    
                File.open(LOG_FILE, 'a'){ |f|
                    f.puts msg
                    f.puts "Args: " + args.join(',')
                }
    
                # While we're in here the daemon is running.
                while running?
                    if state == RUNNING
                        sleep 20 
                        msg = 'Service is running as of: ' + Time.now.to_s
                        File.open(LOG_FILE, 'a'){ |f| f.puts msg }
                    else # PAUSED or IDLE
                        sleep 0.5
                    end
                end
    
                # We've left the loop, the daemon is about to exit.
    
                File.open(LOG_FILE, 'a'){ |f| f.puts "STATE: #{state}" }
    
                msg = 'service_main left at: ' + Time.now.to_s
    
                File.open(LOG_FILE, 'a'){ |f| f.puts msg }
            end
    
            # This event triggers when the service receives a signal to stop. I've
            # added an explicit "exit!" here to ensure that the Ruby interpreter exits
            # properly. I use 'exit!' instead of 'exit' because otherwise Ruby will
            # raise a SystemExitError, which I don't want.
            #
            def service_stop
                msg = 'Received stop signal at: ' + Time.now.to_s
                File.open(LOG_FILE, 'a'){ |f| f.puts msg }
                exit!
            end
    
            # This event triggers when the service receives a signal to pause. 
            #
            def service_pause
                msg = 'Received pause signal at: ' + Time.now.to_s
                File.open(LOG_FILE, 'a'){ |f| f.puts msg }
            end
    
            # This event triggers when the service receives a signal to resume
            # from a paused state.
            #
            def service_resume
                msg = 'Received resume signal at: ' + Time.now.to_s
                File.open(LOG_FILE, 'a'){ |f| f.puts msg }
            end
        end
    
        # Create an instance of the Daemon and put it into a loop. I borrowed the
        # method name 'mainloop' from Tk, btw.
        #
        DemoDaemon.mainloop
    rescue Exception => err
        File.open(LOG_FILE, 'a'){ |fh| fh.puts 'Daemon failure: ' + err }
        raise
    end
    

    将两个文件放在同一目录中并运行

    ruby demo_daemon_ctl.rb install
    
        3
  •  2
  •   Toad    16 年前
        4
  •  0
  •   Community CDub    8 年前

    一段时间前,我还尝试在Windows上安装Redmine。但我没能让它工作,可能吧 because of lack of Rails knowledge .

    然后我发现 Bitnami Redmine Stack . 他们有一个Windows安装程序,可以安装具有所有必需依赖项的Redmine, 它只是起作用 .

        5
  •  0
  •   James Tan    11 年前

    对于Bohdan建议的Rails 4.0.x应用程序, 我们必须更换

    用rbconfig::config['bindir']配置['bindir']

    记得: gem安装win32服务

        6
  •  0
  •   haiwuxing    8 年前
    • gem install win32-service
    • 放在service.rb文件中的ruby代码下面,更新redmine_dir路径以适合您的redmine安装
    • 创建服务,例如 sc create redmine binPath= "C:\Ruby23-x64\bin\rubyw -C E:\www\redmine-3.3.2\ service.rb" 哪里 E:\www\redmine-3.3.2\ 是service.rb文件所在目录的路径,并且 C:\Ruby23-x64\bin\rubyw 您的Ruby安装路径

    开始 需要“win32/daemon” 包括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
    
    • 注意service.rb中的process.spawn使用完整路径。
        7
  •  0
  •   Gico    6 年前

    希望这对任何人都有帮助。我定义了用瘦服务器启动redmine的Windows服务。

    使用 http://nssm.cc/usage 用于创建Windows服务。将路径设置为ruby.exe,即您的redmine工作目录,并定义启动参数:

    Path: C:\RailsInstaller\Ruby2.3.3\bin\ruby.exe
    Startup directory: C:\Program Files\redmine-3.4.6
    Arguments: C:\RailsInstaller\Ruby2.3.3\bin\thin start -e production -p 3000