如何用Capistrano在服务器上执行命令?

我有一个非常简单的任务叫做update_feeds:

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

每当我尝试运行这个任务,我收到以下消息:

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

我想这是因为我不在正确的目录,但尝试

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

代替

 run "cd #{release_path}" 

也失败了。 手动运行完全相同的命令(通过ssh)完美地工作。 为什么不能正确地cd (改变目录)进入站点目录运行命令?

谢谢!

每个run命令基本上都在其自己的shell环境中执行。 所以你需要做一些事情:

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

但是,您无法 script/console方式运行命令因为script/console用途是交互式的

你想要的是script/runner如此:

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

我希望有帮助。

你应该使用:

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

随着capistrano 3.x

这样做的正确方法是在内部使用:

 within variable_with_the_folder_path do execute :command, parameter end 

例如:

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