【问题标题】:How do I kill a rails webrick server?如何杀死 Rails webrick 服务器?
【发布时间】:2015-08-19 15:10:54
【问题描述】:

当我尝试通过rails s 启动服务器时,我收到以下错误消息:

C:\Users\Frankie\Documents\stocktracker>rails s
=> Booting WEBrick
=> Rails 3.2.8 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
A server is already running. Check C:/Users/Frankie/Documents/stocktracker/tmp/p
ids/server.pid.
Exiting

server.pid 中列出的号码是 8436。

如何手动终止此进程?如何轻松杀死当前运行的所有 webrick 服务器?

【问题讨论】:

标签: windows ruby-on-rails-3 webrick


【解决方案1】:

您可以使用 taskkill 实用程序。

taskkill /PID 8436

【讨论】:

  • 我已经尝试过了,但显然该过程不存在。 ERROR: The process "8436" not found.
  • 那么有可能进程已经死了,无法清理它的pid文件。我会手动删除 pid 文件并重试。
  • @effbott PID 文件在哪里?
  • @LearningROR ~/<YOUR APP FOLDER>/tmp/pids
【解决方案2】:

如果您在 OSX 上使用 iTerm2,您可以打开 Toolbelt => ShowToolbelt,选择 ruby​​ pid 8436 然后单击发送信号将其终止。有时任务杀死对我不起作用。

另外,您可以ps -aux | grep rails 来查找 pid。然后像其他答案推荐的那样杀死。

【讨论】:

    【解决方案3】:

    以下任务定义适用于我(将其放在 lib\tasks 文件夹中的 *.rake 文件中):

    namespace :server do
    
      # ---------------------------------------------------------------------------
      desc "Clear the previous server instance clutter."
      task :cleanup => :environment do      
        pidfile = 'tmp/pids/server.pid'
        if File.exists? pidfile
          pid = File.read(pidfile).to_i
          if RbConfig::CONFIG['host_os'] =~ /mswin32/
            sh "taskkill /f /pid #{pid}"
            sh "del tmp\\pids\\server.pid"
          else
            sh "kill #{pid}"
            sh "rm #{pidfile}"
          end
          puts "All cleaned up. Yay!"
        else
          puts "Already clean. Whew!"
        end
      end
      # ---------------------------------------------------------------------------
      desc "Start an instance of the server cleanly."
      task :startup => :cleanup do
        sh "rails server"
      end
      # ---------------------------------------------------------------------------
    end
    

    现在运行

    rake server:startup
    

    在尝试再次运行 rails 服务器之前,它会清理 Windoze 上的所有剩余进程和 pid 文件。

    【讨论】:

      【解决方案4】:

      对于Linux/Ubuntu 用户,ubuntu 有 kill 命令。在运行webrick 服务器时,在位置APP_DIR/tmp/pids/server.pid 内的项目目录中将保存所有进程ID。
      您只需要打开文件,您就会找到当前正在运行的服务器的进程 ID。现在你可以使用下面的命令来杀死进程

      $ kill [pid] # Example kill 8123
      

      【讨论】:

      【解决方案5】:

      按照以下步骤操作:

      1.Find 'rails s' process id by: ps -aux | grep 轨道

      2.使用带有-9选项的kill命令:kill -p [PID]

      你不会失望的!

      【讨论】: