以下任务定义适用于我(将其放在 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 文件。