Executing rake task through Cronjob in rails for differnet environments
Cronjob: The software utility Cron is a time-based job
scheduler in Unix-like computer operating systems. People who set up and
maintain software environments use cron to schedule jobs (commands or
shell scripts) to run periodically at fixed times, dates, or intervals.
Cronjob's are different for every user You can check your cronjob as
> crontab -l
or you can edit your crontab in one of your editor
> EDITOR=nano crontab -e
A file would open like this:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
26 12 * * * /bin/bash -l -c 'cd /home/<user>/<project>/ && /home/user/.rvm/gems/ruby-2.2.0-preview2@<gemset>/bin/rake RAILS_ENV=development mail_scheduler_reminder_mail'
You can write your own cronjob in place of last line or you can write multiple cronjob's
>Writing a cronjob
1.)Define your time when you want this cronjob to be executed.(26 12 * * *)
2.)Define where your command should be executed.(/home/<user>/<project>/)
3.)Define what command you want to execute.(/home/user/.rvm/gems/ruby-2.2.0-preview2@<gemset>/bin/rake RAILS_ENV=development mail_scheduler_reminder_mai)
This was all about cronjob now write your rake task to be executed by your cronjob
for example:
lib/tasks/mail_scheduler.rake
namespace :mail_sheduler do
desc "Send mail to reporting manager within an hour every time" task reminder_email: :environment do
ReportingManager.all.each do |r| puts "Sending mail..."
UserMailer.reporting_manager_welcome_email(r.email,r.location).deliver
puts "Mail is sent!!!"
end
end
end
Now you have written your rake task and now you need to define this task in your cronjob as:
26 12 * * * /bin/bash -l -c 'cd /home/<user>/<project>/ && /home/user/.rvm/gems/ruby-2.2.0-preview2@<project>/bin/rake RAILS_ENV=development mail_scheduler_reminder_mail'
Comments
Post a Comment