Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chef can't create cron

I tried the simplest example given by http://docs.opscode.com/resource_cron.html#examples

    cron "name_of_cron_entry" do
        hour "8"
        weekday "6"
        mailto "[email protected]"
        action :create
    end

I use knife ssh to make the recipe run on a chef client, and the client gives error:

Error executing action create on resource cron[name_of_cron_entry]
Error updating state of name_of_cron_entry, exit: 1

has anyone met the same problem before? Any solutions?

like image 352
user1411817 Avatar asked May 21 '13 04:05

user1411817


1 Answers

I stumbled into this same problem and found that the example I was using had newlines in it. The problem was that the cron provider runs crontab -u user - and pipes STDIN to the process. It appears that it doesn't accept newlines well for whatever reason.

In /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/provider/cron.rb, I found this:

status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
  stdin.write crontab
    end
    if status.exitstatus > 0
      raise Chef::Exceptions::Cron, "Error updating state of #{@new_resource.name}, exit: #{status.exitstatus}"
    end

Strangely enough, running your example seems to work for me, and creates an empty crontab entry for root:

root@chef-test-01:~# crontab -l
# Chef Name: name_of_cron_entry
[email protected]
* 8 * * 6

This suggests to me that you are using a different version of the chef gem & cron provider.

So, depending on the version of the chef gem you are using, it could be this bug

For reference, the example that didn't work for me was based on this one:

cron "cookbooks_report" do
  action node.tags.include?('cookbooks-report') ? :create : :delete
  minute "0"
  hour "0"
  weekday "1"
  user "opscode"
  mailto "[email protected]"
  home "/srv/opscode-community-site/shared/system"
  command %Q{
    cd /srv/opscode-community-site/current &&
    env RUBYLIB="/srv/opscode-community-site/current/lib"
    RAILS_ASSET_ID=`git rev-parse HEAD` RAILS_ENV="#{rails_env}"
    bundle exec rake cookbooks_report
  }
end

The command I was running was:

  command %Q{
    cd /path/to/src/my-project &&
    bundle exec my_script.rb
  }

There were 2 fixes that worked for me:

  1. Use command 'cd /path/to/src/my-project && bundle exec my_script.rb'
  2. Change from cron provider to cron_d provider from the cron cookbook

Note: I don't think the documentation for either provider in chef is correct however... newlines are not supported in the crontab format unless they are escaped with a backslash \. In my testing with the second fix above, the resulting crontab was not valid and had newlines between commands that were not escaped.

like image 68
TrinitronX Avatar answered Sep 28 '22 15:09

TrinitronX