Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continue when script fails(errors) and capture the output using Capistrano 3

In Capistrano 2.x you could simply add :on_error => :continue like this:

task :bad_script, :on_error => :continue do
    my_error = capture('/path/to/tomcat/shutdown.sh')
end

I don't see any way to do this in Capistrano 3.x or ssh-kit (the underlying communication.) Any help would be appreciated.

task :bad_script do
  server_is_down
    on roles :all do
      begin
        server_is_down = capture('/path/to/tomcat/shutdown.sh')
      rescue
        #do something if server_is_down has the correct text
      end
    end
  end
end

I've tried surrounding the new way in begin/rescue blocks but that only stops it from erroring but it does not return the output from the error.

I'd still like to know how to do this but I figured out a way around needing it for my one case and that is to just set server is down if it fails.

task :bad_script do
  server_is_down = false
    on roles :all do
      begin
        execute('/path/to/tomcat/shutdown.sh')
      rescue
        server_is_down = true
      end
    end
  end
end

This is assuming that it only errors when the shutdown takes place.

like image 639
Richie Rich Avatar asked Jan 14 '14 20:01

Richie Rich


1 Answers

You can suppress the error and redirects stderr to a variable like this:

capture('output_from_tomcat_shutdown=`/path/to/tomcat/shutdown.sh 2>&1` || echo $output_from_tomcat_shutdown')
like image 91
user2342460 Avatar answered Oct 15 '22 01:10

user2342460