diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d86ee96..6bb9df06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ * Serve purely static folders directly (without source/ and config.rb) * Set ignored files and disable directory_indexes from YAML frontmatter * Automatically load helper modules in helpers/ directory +* Add pid for cleanup 2.0.14 ==== diff --git a/middleman-core/lib/middleman-core/watcher.rb b/middleman-core/lib/middleman-core/watcher.rb index aa5ecec3..8888d9b6 100644 --- a/middleman-core/lib/middleman-core/watcher.rb +++ b/middleman-core/lib/middleman-core/watcher.rb @@ -3,6 +3,8 @@ require "net/http" require "win32/process" if ::Middleman::WINDOWS +require "fileutils" + module Middleman class Watcher class << self @@ -63,12 +65,26 @@ module Middleman if @options[:"disable-watcher"] bootup else + pid_name = ".mm-pid-#{@options[:port]||4567}" + + if File.exists?(pid_name) + current_pid = File.open(pid_name, 'rb') { |f| f.read } + begin + Process.kill("INT", -current_pid.to_i) + rescue + ensure + FileUtils.rm(pid_name) + end + end + @server_job = fork { trap("INT") { exit(0) } trap("TERM") { exit(0) } trap("QUIT") { exit(0) } bootup } + + File.open(pid_name, "w+") { |f| f.write(Process.getpgrp) } end end