我正在关注 Ryan Bates 在 Monit 上的 Railscast http://railscasts.com/episodes/375-monit?view=asciicast,Rails应用程序的监控系统。在其中,他为 monit 创建了一个 capistrano 配方(见下文),该配方使用 command 运行cap monit:setup
。当我运行它时,它会失败并显示此消息
command finished in 200ms
failed: "sh -c 'sudo -p '\\''sudo password: '\\'' service monit reload'" on 192.XXX.XXX.XXX
这显然是与密码相关的错误,但我无法弄清楚为什么会发生这种情况,因为脚本运行的早期命令start stop restart syntax reload
似乎运行成功,直到它到达reload
.
注意到脚本中的倒数第二行使用root
,我认为这也可能是一个root
问题,因为我使用不同的用户名(但具有 sudo 权限)登录到我的服务器。因此,我root
在倒数第二行更改为我的用户名并cap monit:setup
再次运行,但我得到了同样的错误。
* 2013-07-10 05:52:01 executing `monit:syntax'
* executing "sudo -p 'sudo password: ' service monit syntax"
servers: ["192.XXX.XXX.XXX"]
[192.XXX.XXX.XXX] executing command
** [out :: 192.XXX.XXX.XXX] Control file syntax OK
command finished in 248ms
* 2013-07-10 05:52:02 executing `monit:reload'
* executing "sudo -p 'sudo password: ' service monit reload"
servers: ["192.XXX.XXX.XXX"]
[192.XXX.XXX.XXX] executing command
** [out :: 192.XXX.XXX.XXX] Usage: /etc/init.d/monit {start|stop|restart|force-reload|syntax}
command finished in 200ms
failed: "sh -c 'sudo -p '\\''sudo password: '\\'' service monit reload'" on 192.XXX.XXX.XXX
Monit.rb 配方
namespace :monit do
desc "Install Monit"
task :install do
run "#{sudo} apt-get -y install monit"
end
after "deploy:install", "monit:install"
desc "Setup all Monit configuration"
task :setup do
monit_config "nginx"
syntax
reload
end
after "deploy:setup", "monit:setup"
%w[start stop restart syntax reload].each do |command|
desc "Run Monit #{command} script"
task command do
run "#{sudo} service monit #{command}"
end
end
end
def monit_config(name, destination = nil)
destination ||= "/etc/monit/conf.d/#{name}.conf"
template "monit/#{name}.erb", "/tmp/monit_#{name}"
run "#{sudo} mv /tmp/monit_#{name} #{destination}"
run "#{sudo} chown root #{destination}"
run "#{sudo} chmod 600 #{destination}"
end