From 98f9443810e62a26006030e5cf1e682a8447dbed Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 23 Oct 2015 22:11:44 +0100 Subject: introduce possibility to run commands in background via nohup --- rubyfy.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/rubyfy.rb b/rubyfy.rb index 2bf679a..4ce5362 100755 --- a/rubyfy.rb +++ b/rubyfy.rb @@ -59,6 +59,7 @@ class Rubyfy work_q = Queue.new servers.each do |server| job = { + :BACKGROUND => @conf["background"], :COMMAND => @conf["command"], :PRECONDITION => @conf["precondition"], :ROOT => @conf["root"], @@ -100,14 +101,25 @@ class Rubyfy private - def run_command(server, command="id", root=false, user=ENV["USER"]) + def run_command(server, command="id", background=false, root=false, user=ENV["USER"]) log(:VERBOSE,"#{server}::Connecting") sudo = root ? "sudo " : "" + if background + nohup = "nohup " + nohup_end = " &" + else + nohup = nohup_end = "" + end Net::SSH.start(server, user) do |session| - exec_command = "#{sudo}sh -c \"#{command}\"" + exec_command = "#{nohup}#{sudo}sh -c \"#{command}\"#{nohup_end}" log(:VERBOSE, "#{server}::Executing #{exec_command}") session.exec!(exec_command) do |channel, stream, data| log(:OUT, "#{server}::#{data}") unless @conf["silent"] + if background + # Give time to attach tty to background + sleep(3) + return + end end end end @@ -127,7 +139,7 @@ private if File.exists?("#{server}.ignore") log(:INFO, "#{server}::Ignoring this server") else - run_command server, command, job[:ROOT], job[:USER] + run_command server, command, job[:BACKGROUND], job[:ROOT], job[:USER] end job[:STATUS] = :OK end @@ -171,6 +183,7 @@ begin [ "--timestamp", "-t", GetoptLong::OPTIONAL_ARGUMENT ], [ "--user", "-u", GetoptLong::OPTIONAL_ARGUMENT ], [ "--verbose", "-v", GetoptLong::OPTIONAL_ARGUMENT ], + [ "--background", "-b", GetoptLong::OPTIONAL_ARGUMENT ], ) Rubyfy.new(opts).run -- cgit v1.2.3