I have one job in schedule.rb:
set :output, File.expand_path('../log/whenever.log', __FILE__)
set :job_template, "bash -l -c 'source ~/.bashrc ; :job'"
every 1.day, :at => '12:01 am' do
runner "MyModel.do_something"
end
In my staging deployment (bash) script I have this line to write to cron:
ssh $SERVER "cd $DEPLOY_TO && whenever --set environment=staging -w"
And this line in the production deployment script:
ssh $SERVER "cd $DEPLOY_TO && whenever --set environment=production -w"
This works fine and creates the job when I deploy either environment. The problem is that whenever sees them both as one job so it gets overwritten by whichever environment was last deployed:
# Begin Whenever generated tasks for: /Users/simon/apps/myapp/staging/config/schedule.rb
1 0 * * * bash -l -c 'source ~/.bashrc ; cd /Users/simon/apps/myapp/staging && script/rails runner -e staging 'MyModel.do_something' >> /Users/simon/apps/myapp/staging/log/whenever.log 2>&1'
# End Whenever generated tasks for: /Users/simon/apps/myapp/staging/config/schedule.rb
and…
# Begin Whenever generated tasks for: /Users/simon/apps/myapp/production/config/schedule.rb
1 0 * * * bash -l -c 'source ~/.bashrc ; cd /Users/simon/apps/myapp/production && script/rails runner -e production 'MyModel.do_something' >> /Users/simon/apps/myapp/production/log/whenever.log 2>&1'
# End Whenever generated tasks for: /Users/simon/apps/myapp/production/config/schedule.rb
What’s a sensible way to add the same cron job for two separate environments on the same server?
You can namespace your whenever tasks by using something similar to the following:
In the above example,
stageis the variable that contains the environment. Change it to whatever you are using.The Capistrano integration section at https://github.com/javan/whenever goes into a bit more detail if you need it.