3

I am using:

Ruby     1.9.3
whenever 0.9.4
Rails    3.2
and capistrano/whenever extension.

Whenever the deployment happens, it adds entries to the crontab file after each deployment. Since 5 deployments, there are 40 entries in crontab -l, as schedule.rb has 8 cron entries. For each release there are different entries. Should it not overwrite the entries by default?

It recreates entries everytime mentioned in schedule.rb file.

Les Nightingill
  • 5,662
  • 1
  • 29
  • 32
Sid
  • 4,893
  • 14
  • 55
  • 110
  • Yes, it should be overwriting them. Could you please include some more information which may help us diagnose your problem? What is in your `schedule.rb`? What is in the `crontab`? Do you have any unusual deployment code? What happens if you empty the `crontab` and re-deploy on the clean system? ....... – Tom Lord Jul 15 '16 at 08:18
  • The schdule.rb defines 8 simple entries to run a command once a day. e.g. `every 1.day do end` I cleared the crontab and triggered deployment twice. It added the cron entries two times. – Sid Jul 15 '16 at 08:50
  • Please edit the original post, with the actual code. (If it contains sensitive information, you can edit it with dummy data so long as the meaning is not lost.) – Tom Lord Jul 15 '16 at 09:02
  • Did you ever figure out this issue? – Catfish May 18 '17 at 02:55
  • Actually yes. I will post an answer. – Sid May 18 '17 at 14:50
  • would love to see your solution, Sid – Les Nightingill May 27 '17 at 04:06

2 Answers2

4

I found that whenever was adding a cron job to the crontab file, with each cron job delimited by a comment line that includes the path to the capistrano releases directory... something like this:

# Begin Whenever generated tasks for: /home/path/www/to/releases/2070527160502/config/schedule.rb

(You can look at the raw crontab file with crontab -e to see what whenever has put in there)

When the next deploy occurs, whenever looks to see if there are comment-delimited cron jobs, but using the new release number. It doesn't find that, so it appends new jobs to the crontab file.

My workaround for this problem is to specify the update_crontab task in deploy.rb with the explicit path to schedule.rb like this:

namespace :deploy do
  task :update_crontab do
    on roles(:all) do
      within current_path do
        execute :bundle, :exec, :whenever, "--update-crontab", "~/path/to/current/config/schedule.rb"
      end
    end
  end
end
after 'deploy:symlink:release', 'deploy:update_crontab'

So the comment delimiters in the crontab file contain the 'current' path and not the 'releases/nnnnnnnnnnn' path.

I suspect that this should not be necessary, but after trying to solve the problem for some time, this what what I ended up with.

oma
  • 38,642
  • 11
  • 71
  • 99
Les Nightingill
  • 5,662
  • 1
  • 29
  • 32
1

Check if you are setting

set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }

If you do not it will pick the default identifier, which is the expanded path of your config/schedule.rb file.

https://github.com/javan/whenever/blob/6e69dd8a6b3e2a8f4b2911b4efa5aab65cdc9dcb/lib/whenever/command_line.rb#L51

File.expand_path(@options[:file])
fabro
  • 731
  • 6
  • 11