Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cron function not working #629

Closed
karthickms opened this issue Jun 10, 2016 · 12 comments
Closed

Cron function not working #629

karthickms opened this issue Jun 10, 2016 · 12 comments

Comments

@karthickms
Copy link

Hi All,

I use whenever gem but the cron job is not working for me on ubuntu production server. I used following command for every 2 minutes send the mail

02 * * * * /bin/bash -l -c 'cd /root/peatio/current && RAILS_ENV=production && bundle exec rake dailymail:email_sender --silent'

@benlangfeld
Copy link
Collaborator

Please define "not working". Without such a definition (include cron logs at least) it is impossible to help you.

@karthickms
Copy link
Author

karthickms commented Jun 10, 2016

Hi,

I have used following code for send mail,
in config/schedule.rb

every 1.hours do
  rake 'dailymail:email_sender'
end

in lib/tasks/dailymail.rake

namespace :dailymail do
  desc "Send email to users"
  task :email_sender => :environment do
  Member.all.each do |members|
    DailyemailMailer.dailymail.deliver
  end
  end
end

in app/mailers/dailyemail_mailer.rb

class DailyemailMailer < ActionMailer::Base
  default from: 'support@tradenaira.com'

  def dailymail(member)
        mail to: member.email, subject: 'TradeNAIRA daily rates update'
  end

end

When I enter the crontab -l in terminal, list out the follows,

# Begin Whenever generated tasks for: /root/peatio/current/config/schedule.rb
0 * * * * /bin/bash -l -c '/usr/local/rbenv/shims/backup perform -t database_backup'

10 20 * * * /bin/bash -l -c 'cd /root/peatio/current && RAILS_ENV=production bundle exec rake solvency:clean solvency:liability_proof --silent'

02 * * * * /bin/bash -l -c 'cd /root/peatio/current && RAILS_ENV=production && bundle exec rake dailymail:email_sender --silent'

# End Whenever generated tasks for: /root/peatio/current/config/schedule.rb

What should I do ?

@benlangfeld
Copy link
Collaborator

Please confirm the following:

  1. Does the mailer work if you invoke the rake task manually?
  2. Do your other two cron jobs run successfully?
  3. What is printed in your application log and/or cron log (/var/log/cron.log) at the time the mailer job runs?
  4. What happens if you change the schedule to every :hour do?

@karthickms
Copy link
Author

  1. If I manually enter the "bundle exec rake dailymail:email_sender" in terminal. I got mail.
  2. This is a existing application. So I am not sure the above two cron is run or not
  3. Sorry I dont know where is cron.log but the cron.log is no on /var/log/
  4. Same as working. when i change to every :hour

Please help me.

@benlangfeld
Copy link
Collaborator

benlangfeld commented Jun 10, 2016

So I am not sure the above two cron is run or not

Please find out. I can't help you otherwise.

Sorry I dont know where is cron.log but the cron.log is no on /var/log/

What OS are you using? Could you perhaps consult its documentation to find your cron log?

Same as working. when i change to every :hour

I don't understand. It does or does not work? Is there any difference in the generated crontab?

@karthickms
Copy link
Author

karthickms commented Jun 10, 2016

I am using Ubuntu server.

And when i enter the "whereis cron.log" in terminal

It should be displays :
cron: /usr/sbin/cron /etc/cron.weekly /etc/cron.monthly /etc/cron.d /etc/cron.hourly /etc/cron.daily /usr/share/man/man8/cron.8.gz

It's not working. It is working only when i enter in terminal like the command "bundle exec rake 'dailymail:email_sender'".

@benlangfeld
Copy link
Collaborator

You might want to look into explicitly logging your cron jobs and remove the --silent option to get more verbose output. Also try looking in /var/log/syslog for cron output. I also still need to know if the other two jobs work correctly.

@swapnilchincholkar
Copy link

Isn't this is code issue?

Looks like,

def dailymail(member) 
  .....
end

accepts member object & you are not passing that one from rake task. Instead having code

Member.all.each do |members|
    DailyemailMailer.dailymail.deliver
end

should be

Member.all.each do |member|
    DailyemailMailer.dailymail(member).deliver
end

Try to look at the logs, you can see the error else execute below on rails console to see the error

DailyemailMailer.dailymail.deliver

@karthickms
Copy link
Author

Hi @swapnilchincholkar,

Yes. I have missed the code the your pointed out on my second chatting in this loop. But on my code i have correctly do it. Like as

DailyemailMailer.dailymail(member).deliver

@karthickms
Copy link
Author

karthickms commented Jun 13, 2016

Hi @benlangfeld,

Yes. I am checked on the your specified path /var/log/syslog. In that my cron is displayed. But mail is not receiving

What should I do? Please help me

@karthickms
Copy link
Author

Hi All,

I am using following line for that in schedule.rb. It's working fine

env :PATH, ENV['PATH']
env :GEM_PATH, ENV['GEM_PATH']

@Deepikapc
Copy link

Still issue is not solved! after adding path in schedule.rb file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants