1

I have written this method in my model :-

class Message < ActiveRecord::Base
  def self.create_message
    ShortMessage.create(message: "cron job")
    puts "created"
  end
end

And then I updated schedule.rb file :-

every 1.minute do
  runner "Message.create_message", :environment => "development"
end

And finally executed all this cammands :-

whenever --update-crontab
tail -f /var/log/syslog | grep CRON

But nothing is created in my database, when checked from console ShortMessage.last

code_aks
  • 1,972
  • 1
  • 12
  • 28
  • What is `ShortMessage`? Any validations on the model? How does your log file look like? – spickermann Feb 24 '19 at 08:04
  • 1
    No it is only table name i.e. short_messages. – code_aks Feb 24 '19 at 08:08
  • Check if it's running by `crontab -l` – dan-klasson Feb 24 '19 at 08:37
  • Please attach `crontab -l` and `which ruby` output – Vasfed Feb 24 '19 at 09:22
  • What is runner name for `Message` class? Is it `message.rb` ? – zeitnot Feb 24 '19 at 11:39
  • @zeitnot, Yes it is message.rb file – code_aks Feb 24 '19 at 16:59
  • @dan-klasson, this is what I get when I run "tail -f /var/log/syslog | grep CRON" cammand :- Feb 24 22:31:01 Sonu-pc CRON[23393]: (sonu) CMD (/bin/bash -l -c 'cd /home/sonu/Desktop/voice_rules && bundle exec bin/rails runner -e development '\''Message.create_message'\''') – code_aks Feb 24 '19 at 17:03
  • And also when I run this cammand from terminal then it executed perfectly(means short_message is created in DB) :- /bin/bash -l -c 'cd /home/sonu/Desktop/voice_rules && bundle exec bin/rails runner -e development '\''Message.create_message'\''' – code_aks Feb 24 '19 at 17:04

2 Answers2

1

Cron does not initialise full session, if you're using ruby version manager like rvm - it may not get loaded, for using in cron you need ruby wrappers or load rvm, for more details look into rvm documentation about cron, command for cron will look like

cd (working-dir) && /usr/local/rvm/wrappers/default/bundle bin/rails runner -e development '\''Message.create_message'\'''
Vasfed
  • 18,013
  • 10
  • 47
  • 53
0

Clear existing cron jobs.

crontab -r

Update cronjob with the environment.

whenever --update-crontab --set environment='development'