Unable to run cronjob using whenever in rails 3 - ruby-on-rails

i follow all the steps for whenever with reference to https://github.com/javan/whenever
in schedule.rb
require 'yaml'
set :environment, 'production'
set :output, {
:error => "/log/error.log",
:standard => "/log/cron.log"
}
every 1.minute do
runner "User.weekly_update"
end
in gemfile
gem 'whenever', :require => false
output of some command
localhost:~/project$ whenever -i
[write] crontab file updated
localhost:~/project$ crontab -l
# Begin Whenever generated tasks for: /home/bacancy/project/config/schedule.rb
* * * * * /bin/bash -l -c 'cd /home/bacancy/project && script/rails runner -e production '\''User.weekly_update'\'' >> /log/cron.log 2>> /log/error.log'
# End Whenever generated tasks for: /home/bacancy/project/config/schedule.rb
# Begin Whenever generated tasks for: store
* * * * * /bin/bash -l -c 'cd /home/bacancy/project && script/rails runner -e development '\''User.weekly_update'\'''
# End Whenever generated tasks for: store
# Begin Whenever generated tasks for: lapulguilla
# End Whenever generated tasks for: lapulguilla
and then i type
localhost:~/project$ whenever
* * * * * /bin/bash -l -c 'cd /home/bacancy/project && script/rails runner -e production '\''User.weekly_update'\'' >> /log/cron.log 2>> /log/error.log'
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.
In User model i have definition self.weekly_update
def self.weekly_update
puts "cronjobs is called in every minutes"
end

Your everything is correct, its just that you are saying to cron job to print something in background, so it is doing that and you are able to observe it.
Instead you should try create or destroy some records to feel the changes i think :)

Related

Whenever using development database in production

I am using Whenever v0.9.7 with Rails 4.2.4. whenever is using development database in production also.
When i execute whenever i get following which shows production. But the time retrieved here is from development database and not production.
45 10 * * * /bin/bash -l -c 'cd /data/swati/Projects/metal && bundle exec bin/rails runner -e production '\''Mixins::UploadMaster.new.upload_data(ErpFile.find(4).table_name, ErpFile.find(4).file_name)'\'' >> /data/swati/Projects/metal/log/cron_log.log 2>&1'
Can anyone help resolve the issue?
Here is my schedule.rb
require File.expand_path('../environment', __FILE__)
set :output, ENV["CRON_LOG_FILE"]
erp_access = ErpAccess.first
erp_access.erp_files.each do |erp_file|
if erp_file.start_time
if erp_file.repeat_hours
every erp_file.repeat_hours.hours, at: erp_file.start_time do
toRun = 'Mixins::UploadMaster.new.upload_data(ErpFile.find(' + erp_file.id.to_s + ').table_name, ErpFile.find('+ erp_file.id.to_s + ').file_name)'
runner toRun
end
end
end
end

Capistrano Cron Rails - Crontab created, but not running

Capistrano, Whenever, Rails.
Using Whenever and Capistrano I pushed new data to the crontab on my server.
$ crontab -l
# Begin Whenever generated tasks for: myAPP
* * * * * /bin/bash -l -c 'cd /home/.../apps/myAPP/releases/20184..1433 && bin/rails runner -e production '\''myModel.new.HELLO()'\'''
# End Whenever generated tasks for: myAPP
myModel -> HELLO()
def HELLO()
p "HELLO"
end
How do I know if this is working? I don't think it is because the logs are blank. What am I doing wrong?
thanks
p "HELLO" just prints to stdout, which doesn't do much good in a cronjob context. Try this code instead:
def HELLO()
Rails.logger.info "HELLO"
end
This will print the message to the standard Rails log file. By default, this will be logs/production.log in the current release that Capistrano deployed.

Saving a new value to the db using the whenever gem

I'm trying to save a value in my db every hour. Let's say it saves a random string of characters to the letter field in my randomizer table. I have the whenever gem installed and my schedule.rb runs every hour calling a class method in my model. It doesn't seem to save a value the way I have it set up.
Here is my schedule.rb:
set :output, "#{path}/log/cron.log"
set :environment, 'development'
every 1.hour do
runner "Randomizer.rand_letters"
command "echo 'random job attempted.'"
end
Here is my Randomizer model:
class Randomizer < ActiveRecord::Base
def self.rand_letters
#random_string = Randomizer.new(letters: 'asdfg')
#random_string.save
end
end
Here is my output when I run whenever:
0 * * * * /bin/bash -l -c 'cd /home/action/workspace/word-vomit && bin/rails runner -e development '\''Randomizer.rand_letters'\'' >> /home/action/workspace/word- vomit/log/cron.log 2>&1'
0 * * * * /bin/bash -l -c 'echo '\''random job attempted.'\'' >> /home/action/workspace/word-vomit/log/cron.log 2>&1'
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.

Whenever rails gem not working with cronjob.. why?

I am using Whenever gem with rails. For some reason the first cronjob does not work. If i copy paste the command into the shell manually, it works as it should.
The second "touch testing123.txt" also works fine.
I have no idea why the GiftPackage.do_scheduled_deliveries rails method does not run. I'm really at a loss right now.. any help would be greatly appreciated!
# Begin Whenever generated tasks for: /var/www/mysite/releases/20130131200554/config/schedule.rb
*/1 * * * * /bin/bash -l -c 'cd /var/www/mysite/releases/20130131200554 && script/rails runner -e staging '\''GiftPackage.do_scheduled_deliveries'\'''
*/1 * * * * /bin/bash -l -c 'cd /var/www/mysite/releases/20130131200554 && touch testing123.txt'
# End Whenever generated tasks for: /var/www/mysite/releases/20130131200554/config/schedule.rb
Thanks!
:)
Most likely you are getting an error within the app itself.
First step would be, run the task in the console using the staging environment.
Second would be to check the cron logs and see if it is outputting any errors.
You can set whenever to log output by adding the following to your schedule.rb file;
set :output, "/[path to log]/cron.log"
This should help you get closer to finding the solution.
Try setting your PATH variable before the jobs, your cron is probably not able to find ruby :
For instance :
PATH=/usr/local/rvm/gems/ruby-1.9.3-p194/bin:/usr/local/rvm/gems/ruby-1.9.3-p194#global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p194/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/$
# Begin Whenever generated tasks for: /var/www/mysite/releases/20130131200554/config/schedule.rb
*/1 * * * * /bin/bash -l -c 'cd /var/www/mysite/releases/20130131200554 && script/rails runner -e staging '\''GiftPackage.do_scheduled_deliveries'\'''
*/1 * * * * /bin/bash -l -c 'cd /var/www/mysite/releases/20130131200554 && touch testing123.txt'
# End Whenever generated tasks for: /var/www/mysite/releases/20130131200554/config/schedule.rb

whenever gem command not running on intervals

on the whenever gem I am getting everything right . and can udpate the schedule.rb file but not getting logs after every time interval . Here is the code
this is schedule file
every 2.minutes do
command "echo 'you can use raw cron sytax too'"
rake "ftp"
end
This is ftp.rake file in lib/tasks/
require 'net/ftp'
require 'timeout'
require 'pathname'
require 'fileutils'
task :ftp do
command "echo 'you can use raw cron sytax too'"
ftp = nil
f = File.open("/home/hb/demo_haml/log/ftp_transfer.log","w+")
f.write("\n#{Time.now}")
ftp = Net::FTP::new("192.168.x.x")
ftp.login("demo", "demo")
ftp.chdir("/home/hd")
ftp.getbinaryfile("one.conf", "/home/hb/demo_haml/conf/one.conf", 1024)
ftp.close if ftp
f.close
end
After this i get the time on the log for the first time when i fire
whenever --update-crontab store
Running crontab -l
# Begin Whenever generated tasks for: demo_haml
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'echo '\''you can use raw cron sytax too'\'''
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd /home/hb/applications/other_old_apps/Arpit/demo_haml && RAILS_ENV=production bundle exec rake ftp --silent'
What to do to keep on executing after every interval?

Resources