ruby 如何在 Capistrano v3 的服务器上运行 shell 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18838930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run shell commands on server in Capistrano v3?
提问by Jgod
I'm new to Capistrano and I've tried using Capistrano's DSL to run shell commands on the server ('run', 'execute', etc.), but it appears that it was deprecated. After searching and searching for a functional equivalent, I still am lost.
我是 Capistrano 的新手,我尝试使用 Capistrano 的 DSL 在服务器上运行 shell 命令(“运行”、“执行”等),但它似乎已被弃用。在搜索和搜索功能等价物之后,我仍然迷失了方向。
Current code:
当前代码:
desc 'Do something'
task :do_something
execute 'echo sometext'
end
Output:
输出:
cap aborted!
undefined method `execute' for main:Object
/Users/Justin/Dropbox/xxxx/xxxx/xxxx/Capfile:45:in `block (2 levels) in <top (required)>'
/Users/Justin/.rvm/gems/ruby-2.0.0-p247/bundler/gems/capistrano-2dc1627838f9/lib/capistrano/application.rb:12:in `run'
/Users/Justin/.rvm/gems/ruby-2.0.0-p247/bundler/gems/capistrano-2dc1627838f9/bin/cap:3:in `<top (required)>'
/Users/Justin/.rvm/gems/ruby-2.0.0-p247/bin/cap:23:in `load'
/Users/Justin/.rvm/gems/ruby-2.0.0-p247/bin/cap:23:in `<main>'
/Users/Justin/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/Users/Justin/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => deploy:do_something
回答by lmars
In Capistrano v3, you must specify where you want to run the code by calling onwith a list of hostnames, e.g.
在 Capistrano v3 中,您必须通过on使用主机名列表调用来指定要运行代码的位置,例如
task :execute_on_server do
on "[email protected]" do
execute "some_command"
end
end
If you have roles set up, you can use the rolesmethod as a convenience:
如果您设置了角色,则可以使用该roles方法方便:
role :mailserver, "[email protected]"
task :check_mail do
on roles(:mailserver) do
execute "some_command"
end
end
There is some v3 documentation here: http://www.capistranorb.com/
这里有一些 v3 文档:http: //www.capistranorb.com/

