Reputation: 63
I'm trying to set up cron job without using "Whenever" in development environment. I checked log and cron seems working in each 2 minutes but no mail was sent... How can I correctly write the cron job?
models/box.rb:
def self.reminder_mail
@boxes = Box.all
@boxes.each do |box|
if box.status == "PENDING"
NoticeMailer.sendmail_reminder(box).deliver
end
end
end
Crontab
*/2 * * * * ubuntu /home/ubuntu/workspace/box/reminder_mail
cronlog
Feb 25 11:10:01 ubuntu-xenial CRON[4599]: (ubuntu) CMD (ubuntu /home/ubuntu/workspace/box/reminder_mail)
Feb 25 11:10:01 ubuntu-xenial CRON[4598]: (CRON) info (No MTA installed, discarding output)
Feb 25 11:12:01 ubuntu-xenial CRON[4605]: (ubuntu) CMD (ubuntu /home/ubuntu/workspace/box/reminder_mail)
Upvotes: 0
Views: 151
Reputation: 63
I solved this problem!
Important is to write rake PATH in my case /home/ubuntu/.rbenv/shims
directly into PATH and not mention into cron job.
*/2 * * * * ubuntu cd /home/ubuntu/workspace/box && RAILS_ENV=development bundle exec rake reminder_task:reminder_mail >> /home/ubuntu/workspace/box/log/cron.log
Upvotes: 0
Reputation: 345
Your cronlog is giving you explanation: there is no MTA in your development system. MTA stands for Mail Transfer Agent, a system service that relays emails sent by clients like the cron job.
If you can, install and configure postfix, which is a very popular MTA.
Your cron file is wrong, change RAILS_ENV=production to be first, something like this RAILS_ENV=production rake reminder_task:reminder_email
.
Also, if your project is using bundler use RAILS_ENV=production bundle exec rake reminder_task:reminder_email
— this will allow to run your rake task with access to all required gems etc. I think you don't have to change working directory, neither. Instead, put a line like this in the cron file:
HOME=/path/to/your/project
Upvotes: 1
Reputation: 341
there is a workaround you can implement an API and use curl to execute it with a custom authorization inside the method just to authorize the request
then add th curl request inside your crontab
*/2 * * * * curl --silent {you can spesify if it was POST,GET..etc} http://example.com/{your API path}
Upvotes: 0