Opsworks Chef 无法创建 cron

Posted

技术标签:

【中文标题】Opsworks Chef 无法创建 cron【英文标题】:Opsworks Chef unable to create cron 【发布时间】:2017-01-20 08:23:36 【问题描述】:

我尝试使用在 AWS 中运行 opsworks 的厨师创建一个 cron 任务。 我想我有一些权限问题,因为写入失败,但是我在哪里可以增加权限?还是我真的需要增加权限?

厨师:

cron 'crontest' do
  minute "0"
  hour "/2"
  day "*"
  month "*"
  weekday "*"
  command "ls"
end

日志:

Logs from opsworks:
------------------

[2016-09-12T19:47:13+00:00] INFO: Processing cron[crontest] action create (my::crontest line 1)

================================================================================
Error executing action `create` on resource 'cron[crontest]'
================================================================================

Chef::Exceptions::Cron
----------------------
Error updating state of crontest, exit: 1

Resource Declaration:
---------------------
# In /var/chef/runs/de35b7b0-20ad-4903-a5b1-e8e901695b5b/local-mode-cache/cache/cookbooks/my/recipes/crontest.rb

1: cron 'crontest' do
2:   minute "0"
3:   hour "/2"
4:   day "*"
5:   month "*"
6:   weekday "*"
7:   command "ls"
8: end

Compiled Resource:
------------------
# Declared in /var/chef/runs/de35b7b0-20ad-4903-a5b1-e8e901695b5b/local-mode-cache/cache/cookbooks/my/recipes/crontest.rb:1:in `from_file'

cron("crontest") do
action [:create]
retries 0
retry_delay 2
default_guard_interpreter :default
minute "0"
hour "/2"
day "*"
month "*"
weekday "*"
command "ls"
user "root"
declared_type :cron
cookbook_name "my"
recipe_name "crontest"
end

Platform:
---------
x86_64-linux

[2016-09-12T19:47:13+00:00] INFO: Running queued delayed notifications before re-raising exception
[2016-09-12T19:47:13+00:00] ERROR: Running exception handlers
[2016-09-12T19:47:13+00:00] ERROR: Exception handlers complete
[2016-09-12T19:47:13+00:00] FATAL: Stacktrace dumped to /var/chef/runs/de35b7b0-20ad-4903-a5b1-e8e901695b5b/local-mode-cache/cache/chef-stacktrace.out
[2016-09-12T19:47:13+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2016-09-12T19:47:13+00:00] ERROR: cron[crontest] (my::crontest line 1) had an error: Chef::Exceptions::Cron: Error updating state of crontest, exit: 1
[2016-09-12T19:47:13+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)


Logs from /var/chef/runs/de35b7b0-20ad-4903-a5b1-e8e901695b5b/local-mode-cache/cache/chef-stacktrace.out
------------------
>>>> Caused by Chef::Exceptions::Cron: Error updating state of crontest, exit: 1
/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.13.37/lib/chef/provider/cron.rb:232:in `write_crontab'
/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.13.37/lib/chef/provider/cron.rb:157:in `block in action_create'
/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.13.37/lib/chef/mixin/why_run.rb:52:in `add_action'
/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.13.37/lib/chef/provider.rb:176:in `converge_by'
/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.13.37/lib/chef/provider/cron.rb:156:in `action_create'
/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.13.37/lib/chef/provider.rb:145:in `run_action'

【问题讨论】:

hour "/2" 是目标系统的有效 cron 时间吗?某些版本的 cron 不支持重复语法。可能在尝试验证新的 crontab 时失败了。 【参考方案1】:

我最近遇到了 OpsWorks (Chef 12) cron 工作的问题。我试图指定

weekday 'wed,fri'

cron 作业定义中的语法,虽然这在目标系统上是合法的,但它触发了 Chef 方法中的错误。该错误的症状是,每次执行此配方时,当前的 crontab 条目都没有被编辑/替换,在原来的下方添加了一个副本。在我注意到问题之前,我有六行相同的行。

我通过使用

解决了我的具体问题
weekday '4,6' 

语法。我从中吸取的教训是,不要试图对你的 cron 工作配方进行远程聪明。使用最简单、最古老、最无聊的符号来定义您的 cron 作业。

Chef cronjob 代码使用正则表达式来确定要添加/编辑哪些行。应该避免任何可能混淆解析器的事情。

【讨论】:

以上是关于Opsworks Chef 无法创建 cron的主要内容,如果未能解决你的问题,请参考以下文章

AWS Opsworks 中的 Chef 环境

无法在 AWS Opsworks 上使用 Chef 12 查找 Chef 社区食谱

AWS opsworks 上的 chef 12 脚本无法安装 jenkins

安装 Postgresql 时 AWS Opsworks Chef 配方失败

Opsworks Chef 12.0 用一个公用文件管理两本食谱

即使食谱可用并且设置了依赖项,Chef 中也没有此类食谱异常 - AWS OpsWorks