From cfa6e6f2599f9cbc3862e3dc1ab150003823ee4d Mon Sep 17 00:00:00 2001 From: brainopia Date: Sat, 7 Jan 2012 18:19:54 +0700 Subject: [PATCH 1/5] Fix foreman to work with cmds containing pipes and redirects --- bin/runner | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/runner b/bin/runner index 69011a5..65e7c5f 100755 --- a/bin/runner +++ b/bin/runner @@ -1,2 +1,2 @@ #!/bin/sh -exec $1 2>&1 +eval "$1" 2>&1 From baa7b7685c1eebd29d13817b3caeb8872ba1d915 Mon Sep 17 00:00:00 2001 From: brainopia Date: Sat, 7 Jan 2012 20:19:57 +0700 Subject: [PATCH 2/5] Use ruby exec which works with escaped cmd and replaces shell --- bin/runner | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/runner b/bin/runner index 65e7c5f..0fae134 100755 --- a/bin/runner +++ b/bin/runner @@ -1,2 +1,2 @@ -#!/bin/sh -eval "$1" 2>&1 +#!/usr/bin/env ruby +exec "#{ARGV.first} 2>&1" From b561555f3a11cdcd96ce5f158bd20189be36eafa Mon Sep 17 00:00:00 2001 From: brainopia Date: Sun, 8 Jan 2012 09:42:51 +0700 Subject: [PATCH 3/5] Fix for double fork --- lib/foreman/engine.rb | 7 +++++++ lib/foreman/process.rb | 2 ++ 2 files changed, 9 insertions(+) diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index c65a5a5..7fa6fd3 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -72,10 +72,17 @@ private ###################################################################### def kill_all(signal="SIGTERM") running_processes.each do |pid, process| info "sending #{signal} to pid #{pid}" + kill(signal, -pid) or kill(signal, pid) Process.kill(signal, pid) rescue Errno::ESRCH end end + def kill(signal, pid) + Process.kill signal, pid + rescue Errno::ESRCH + false + end + def terminate_gracefully info "sending SIGTERM to all processes" kill_all "SIGTERM" diff --git a/lib/foreman/process.rb b/lib/foreman/process.rb index 06f79f6..e570719 100644 --- a/lib/foreman/process.rb +++ b/lib/foreman/process.rb @@ -30,11 +30,13 @@ private def fork_with_io(command) reader, writer = IO.pipe pid = fork do + Process.setpgrp trap("INT", "IGNORE") $stdout.reopen writer reader.close exec Foreman.runner, replace_command_env(command) end + Process.detach pid [ reader, pid ] end From 64bd4db12898ba9cfdf479ba778dbd307d89cc1f Mon Sep 17 00:00:00 2001 From: brainopia Date: Sun, 8 Jan 2012 10:15:23 +0700 Subject: [PATCH 4/5] In case someone wants to use bin/runner directly --- bin/runner | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/runner b/bin/runner index 0fae134..89257a0 100755 --- a/bin/runner +++ b/bin/runner @@ -1,2 +1,2 @@ #!/usr/bin/env ruby -exec "#{ARGV.first} 2>&1" +exec "#{ARGV.join(' ')} 2>&1" From 66b1483a75f9998cf67250285754fab56c8665c9 Mon Sep 17 00:00:00 2001 From: brainopia Date: Sun, 8 Jan 2012 10:18:48 +0700 Subject: [PATCH 5/5] Remove old cruft --- lib/foreman/engine.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index 7fa6fd3..7e8df1f 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -73,7 +73,6 @@ private ###################################################################### running_processes.each do |pid, process| info "sending #{signal} to pid #{pid}" kill(signal, -pid) or kill(signal, pid) - Process.kill(signal, pid) rescue Errno::ESRCH end end