How to execute a command on the server with Capistrano?

I have a very simple task called update_feeds:

desc "Update feeds"
task :update_feeds do
  run "cd #{release_path}"
  run "script/console production"
  run "FeedEntry.update_all"
end

Whenever I try to run this task, I get the following message:

[out :: mysite.com] sh: script/console: No such file or directory

I figured it's because I am not in the right directory, but trying

run "cd ~/user/mysite.com/current"

instead of

run "cd #{release_path}"

Also fails. Running the exact same commands manually (through ssh) works perfectly. Why can't capistrano properly cd (change directory) into the site directory to run the command?

Thanks!


Each run command basically executes within its own shell environment. So you would need to do something like:

run "cd #{release_path} && script/console production"

However, you cannot run commands in script/console this way as script/console is for interactive usage.

What you want is script/runner like so:

run "cd #{release_path} && script/runner -e production 'FeedEntry.update_all'"

I hope that helps.


You should use:

execute "cd #{release_path} && script/console production"

With capistrano 3.x


The proper way of doing this is using within like that:

within variable_with_the_folder_path do
    execute :command, parameter
end

for example:

    # Bower Cache Clean:
    bower_path = fetch(:bower_path)
    within bower_path do
      execute :node, "#{bower_path_to_bin}", 'cache clean'
    end