def action_create
crontab = String.new
newcron = String.new
cron_found = false
newcron << "# Chef Name: #{new_resource.name}\n"
[ :mailto, :path, :shell, :home ].each do |v|
newcron << "#{v.to_s.upcase}=#{@new_resource.send(v)}\n" if @new_resource.send(v)
end
newcron << "#{@new_resource.minute} #{@new_resource.hour} #{@new_resource.day} #{@new_resource.month} #{@new_resource.weekday} #{@new_resource.command}\n"
if @cron_exists
unless compare_cron
Chef::Log.debug("Skipping existing cron entry '#{@new_resource.name}'")
return
end
status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
stdout.each_line do |line|
case line.chomp
when "# Chef Name: #{@new_resource.name}"
cron_found = true
next
when CRON_PATTERN
if cron_found
cron_found = false
crontab << newcron
next
end
else
next if cron_found
end
crontab << line
end
end
status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
crontab.each_line { |line| stdin.puts "#{line}" }
end
Chef::Log.info("Updated cron '#{@new_resource.name}'")
@new_resource.updated_by_last_action(true)
else
unless @cron_empty
status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
stdout.each { |line| crontab << line }
end
end
crontab << newcron
status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
crontab.each_line { |line| stdin.puts "#{line}" }
end
Chef::Log.info("Added cron '#{@new_resource.name}'")
@new_resource.updated_by_last_action(true)
end
end