namespace :dev do desc 'Run both rails server and webpack dev server' task :run do rails_pid = fork do exec 'rails s' end webpack_pid = fork do exec 'bin/webpack-dev-server' end running = true shutdown = false shutdown_start = nil Signal.trap('SIGINT') do shutdown = true end while running rails_check ||= Process.waitpid(rails_pid, Process::WNOHANG) webpack_check ||= Process.waitpid(webpack_pid, Process::WNOHANG) running = rails_check.nil? || webpack_check.nil? if shutdown if shutdown_start.nil? puts "Shutting down..." shutdown_start = Time.now #Process.kill("SIGINT", rails_pid) rescue Errno::ESRCH #Process.kill("SIGINT", webpack_pid) rescue Errno::ESRCH end if (Time.now - shutdown_start) > 5 if rails_check.nil? puts "Force killing rails..." Process.kill("KILL", rails_pid) end if webpack_check.nil? puts "Force killing webpack..." Process.kill("KILL", webpack_pid) end end end sleep 0.25 end end end