title: Capistrano v3 - Hook to run command post deploy
body: <p>I am using Capistrano to deploy to a server running Nginx. I'm running into some issues with APC and I need to reload PHP-FPM after Capistrano has completed the deployment.  The issue <a href="https://stevegrunwell.com/blog/restart-php-fpm-during-deployments/" rel="noreferrer">itself is outlined here</a>  but like that author I don't want to have to SSH in and reload PHP-FPM remotely from the command line, I'd like Capistrano to do it as a post deployment hook.</p>
  
  <p>The essence of the deploy.rb being used is below;</p>
  
  <pre><code>    set :application, "deploytest"
  
      set :repository,  "git@bitbucket.org:gitaccount/git-repo.git"
      set :scm,         :git
      set :deploy_via,  :remote_cache
      set :app_webroot, "/public"
  
      default_run_options[:pty] = true
  
      desc "Execute Capistrano tasks against Production server."
      task :prod do
          role :web, "123.45.67.89"
          role :app, "123.45.67.89"
          set :env,         "prod"
          set :domain,      "deploy-domain.com"
          set :deploy_to,   "/var/www/vhosts/#{domain}/site"
          set :branch,      "master"
      end
  </code></pre>
  
  <p>And I can push using the command;</p>
  
  <pre><code>    bundle exec cap prod deploy        
  </code></pre>
  
  <p>Works great.  Boy have I struggled trying to get that command to automatically trigger another command once the deploy is complete.</p>
  
  <p><strong>What I have tried;</strong></p>
  
  <p>Here's a summary of the main approaches;</p>
  
  <ol>
  <li><p>Creating a new namespace for my task</p>
  
  <pre><code>namespace :mcnab do
    desc "Running hook post deploy"
    task :fpmreload do
      execute "service php-fpm reload"
    end
  end
  
  after "deploy:create_symlink", "mcnab:fpmreload"
  </code></pre></li>
  <li><p>Wrapping both tasks in a 'deploy' namespace and using the following command to trigger the hook</p>
  
  <pre><code>after "deploy:create_symlink", "deploy:fpmreload"
  </code></pre></li>
  <li><p>Explicitly setting the roles again within the new task</p>
  
  <pre><code>task :fpmreload do
      role :web, "178.62.13.10"
      role :app, "178.62.13.10"
      on roles(:all) do 
          execute "service php-fpm reload"
      end  
  end
  </code></pre></li>
  <li><p>Setting the user explicitly</p>
  
  <pre><code>task :fpmreload do
    on "user@123.45.67.89" do
      execute "service php-fpm reload"
    end
  end
  </code></pre></li>
  <li><p>Using 'run' instead of execute</p>
  
  <pre><code>task :fpmreload do
    on "user@123.45.67.89" do
      run "service php-fpm reload"
    end
  end
  </code></pre></li>
  </ol>
  
  <p>Hrrmph, and about a million variations thereon. I'm really just guessing now, even with verbose error reporting the error messages are not helping much. Just one working example of a deploy.rb file with a simple post deploy hook running a command would be great but I can't find one.</p>