title: Run capistrano tasks as different user
body: <p>I'm a newbie with capistrano, and have been banging my head against this problem for a couple of days so would really appreciate some help.</p>
  
  <p>We have an annoying security setup, which means that i cannot run capistrano tasks on the remote server as the ssh user. It's a simpler version of <a href="https://stackoverflow.com/questions/6578520/run-deployment-task-as-different-users">This problem</a> - which doesn't seem to have a working answer.</p>
  
  <p>Edit:
  Delving further into this, I find that i can do something like:</p>
  
  <pre><code>run "su - newuser" do |channel, stream, data|
  
            channel.send_data("#{password}\n")
  
            channel.send_data("rm -rf #{release_path}\n")
  
          end }
  </code></pre>
  
  <p>I can use this to overwrite the default deploy methods, and do most of what I need to do. I can create the required directories on the server and so on, and pull from git as the ssh user using ssh forwarding.  Which creates my next problem.  I now have directories owned by newuser which contain files and directories owned by ssh_user.  I do have sudo privileges as newuser, but can't work out how to chmod the files.  I've tried stuff like:</p>
  
  <pre><code>run "su - newuser" do |channel, stream, data
  
                channel.send_data("#{password}\n")
  
                channel.send_data("sudo chmod 775 #{cache_path}\n")
  
              end }
  </code></pre>
  
  <p>but everything just hangs, and it seems as if the block is executed in an infinite loop. Obviously there is a lot i don't understand here and some pointers would be much appreciated - even if just to a good relevant read.</p>