我正在编写一个 Puppet 模块来部署 Hyperic,如果服务没有运行,最后一部分是执行hq-agent.sh start。要确定服务是否正在运行,我可以执行hq-agent.sh status,其中包含文本“HQ Agent is running”。
Puppet 文档说明 onlyif 可以工作,但是如果运行需要返回退出代码 1,如果不运行则需要返回 0;什么是适用的 bash(?) 命令来进行转换?
伪代码 => if ('hq-agent.sh status' contains "running") return 1; 否则返回0;
这听起来很像应该通过Service
资源而不是Exec
. 这样做不需要通过系统的常规服务控制子系统(initscripts、systemd 等)来管理服务,尽管我当然会建议您安排这样做,即使您必须自己编写适当的脚本或配置文件. 然而,在这种情况下,该hq-agent.sh
脚本听起来似乎具有与传统 initscript 相似(如果不完全相同)的界面。如果是这样,那么设置为普通系统服务可能会很容易。如果你这样做了,那么管理它就像
service { 'hq-agent':
ensure => 'running',
enable => true,
}
但是如果您只想使用临时脚本来管理服务,Puppet 可以支持。具体而言,该Service
资源已start
,restart
,status
,和stop
属性,使用它可以为管理服务指定任意命令。例如,
service { 'hq-agent':
ensure => 'running',
provider => 'service',
hasstatus => false,
hasrestart => false,
status => 'hq-agent.sh status',
start => 'hq-agent.sh start',
stop => 'hq-agent.sh stop',
path => '/path/to/hyperic/bin',
# no 'enable' attribute specified
}
该特定示例hq-agent.sh
基于它与标准 SysV initscript 的表面相似性,对脚本的退出代码做出了一些假设。具体来说,它假设它们符合LSB 规范。如果实际上它们没有,因此您需要测试脚本的输出而不是其退出代码,那么典型的方法是将输出通过管道传输到grep
. 例如,
status => 'hq-agent.sh status | grep -q running'
但是请注意,您可能需要测试脚本的标准错误而不是其标准输出。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句