厨师不会启动我的 init.d 服务
chef won't start my init.d service
我正在通过此 mongo doc 创建服务。
我的食谱中有这个通过模板安装服务:
service 'disable-transparent-hugepages' do
supports :restart => true, :start => true, :stop => true, :reload => true
action :nothing
end
template 'disable-transparent-hugepages' do
path '/etc/init.d/disable-transparent-hugepages'
source 'disable-transparent-hugepages.erb'
owner 'root'
group 'root'
mode '0755'
notifies :enable, 'service[disable-transparent-hugepages]'
notifies :start, 'service[disable-transparent-hugepages]'
end
当我多次 运行 食谱时,它会将 init.d 脚本拉到正确的位置,但是如果我检查它的状态,我会看到:
确认安装后,我在配方中添加了一行以正常启动服务:
service 'disable-transparent-hugepages' do
action :start
end
但是还没开始。
如果我手动启动它,我可以看到它在工作:
厨师为什么不开始服务?
编辑:好的,所以我发现 this SO question 说我的问题可能是我需要 init.d 脚本(在 link 到 mongo 文档中找到)需要以非 0 return 代码退出。我不确定如何在检查状态时让脚本 return 成为非零代码。
这与脚本支持命令的假设有关。
引用 chef doc 关于 service
resource 关于 supports
属性(最后一行的重点是我的):
supports
Ruby Type: Hash
A list of properties that controls how the chef-client is to attempt
to manage a service: :restart, :reload, :status. For :restart, the
init script or other service provider can use a restart command; if
:restart is not specified, the chef-client attempts to stop and then
start a service. For :reload, the init script or other service
provider can use a reload command. For :status, the init script or
other service provider can use a status command to determine if the
service is running; if :status is not specified, the chef-client
attempts to match the service_name against the process table as a
regular expression, unless a pattern is specified as a parameter
property. Default value: { :restart => false, :reload => false,
:status => false } for all platforms (except for the Red Hat platform
family, which defaults to { :restart => false, :reload => false,
:status => true }.)
当 status
为 true
时,提供商尝试调用它,如果 return 为 0
,则服务应该为 运行。
根据您提供的 link,该脚本仅支持 start
,因此当使用参数状态调用时 return 0。
解决此问题的一种方法是使用更精确的服务定义,如下所示:
service 'disable-transparent-hugepages' do
supports :restart => false, :start => true, :stop => false, :reload => false, :status => false
action :start
end
另一种方法是修复初始化脚本以实现状态命令,return如果文件内容正确则为 0,否则为 1。
我认为沿着这条线作为状态案例的东西(未经测试):
status)
if [ -d /sys/kernel/mm/transparent_hugepage ]; then
thp_path=/sys/kernel/mm/transparent_hugepage
elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
thp_path=/sys/kernel/mm/redhat_transparent_hugepage
else
return 0
fi
re='^(0|no)$'
return [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
;;
我的看法:
因为它不完全是真正的服务,所以我会直接管理这些文件,而不是为此使用伪服务。
我正在通过此 mongo doc 创建服务。
我的食谱中有这个通过模板安装服务:
service 'disable-transparent-hugepages' do
supports :restart => true, :start => true, :stop => true, :reload => true
action :nothing
end
template 'disable-transparent-hugepages' do
path '/etc/init.d/disable-transparent-hugepages'
source 'disable-transparent-hugepages.erb'
owner 'root'
group 'root'
mode '0755'
notifies :enable, 'service[disable-transparent-hugepages]'
notifies :start, 'service[disable-transparent-hugepages]'
end
当我多次 运行 食谱时,它会将 init.d 脚本拉到正确的位置,但是如果我检查它的状态,我会看到:
确认安装后,我在配方中添加了一行以正常启动服务:
service 'disable-transparent-hugepages' do
action :start
end
但是还没开始。
如果我手动启动它,我可以看到它在工作:
厨师为什么不开始服务?
编辑:好的,所以我发现 this SO question 说我的问题可能是我需要 init.d 脚本(在 link 到 mongo 文档中找到)需要以非 0 return 代码退出。我不确定如何在检查状态时让脚本 return 成为非零代码。
这与脚本支持命令的假设有关。
引用 chef doc 关于 service
resource 关于 supports
属性(最后一行的重点是我的):
supports
Ruby Type: HashA list of properties that controls how the chef-client is to attempt to manage a service: :restart, :reload, :status. For :restart, the init script or other service provider can use a restart command; if :restart is not specified, the chef-client attempts to stop and then start a service. For :reload, the init script or other service provider can use a reload command. For :status, the init script or other service provider can use a status command to determine if the service is running; if :status is not specified, the chef-client attempts to match the service_name against the process table as a regular expression, unless a pattern is specified as a parameter property. Default value: { :restart => false, :reload => false, :status => false } for all platforms (except for the Red Hat platform family, which defaults to { :restart => false, :reload => false, :status => true }.)
当 status
为 true
时,提供商尝试调用它,如果 return 为 0
,则服务应该为 运行。
根据您提供的 link,该脚本仅支持 start
,因此当使用参数状态调用时 return 0。
解决此问题的一种方法是使用更精确的服务定义,如下所示:
service 'disable-transparent-hugepages' do
supports :restart => false, :start => true, :stop => false, :reload => false, :status => false
action :start
end
另一种方法是修复初始化脚本以实现状态命令,return如果文件内容正确则为 0,否则为 1。
我认为沿着这条线作为状态案例的东西(未经测试):
status)
if [ -d /sys/kernel/mm/transparent_hugepage ]; then
thp_path=/sys/kernel/mm/transparent_hugepage
elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
thp_path=/sys/kernel/mm/redhat_transparent_hugepage
else
return 0
fi
re='^(0|no)$'
return [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
;;
我的看法:
因为它不完全是真正的服务,所以我会直接管理这些文件,而不是为此使用伪服务。