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
Related
Im' trying to figure out how Whenever gem is working.
I would like to create a User instance every minute.
I put the following in my schedule.rb file :
set :output, "/log/cron_log.log"
set :environment, 'development'
every 1.minute do
rake "User.create!(nni: 'TEST0',
password: '123123',
password_confirmation: '123123',
nomprenom: 'user')"
end
Then I typed whenever --update-crontab and checked with crontab -l and get the following
# Begin Whenever generated tasks for: /home/harri/Documents/argia/config/schedule.rb at: 2018-07-10 17:19:18 +0200
* * * * * /bin/bash -l -c 'cd /home/harri/Documents/argia && RAILS_ENV=development bundle exec rake User.create!(nni: '\''TEST0'\'', password: '\''123123'\'', password_confirmation: '\''123123'\'', nomprenom: '\''user'\'') --silent >> /log/cron_log.log 2>&1'
# End Whenever generated tasks for: /home/harri/Documents/argia/config/schedule.rb at: 2018-07-10 17:19:18 +0200
Then I start my localhost and nothing happens, nothing in the log and it doesn't create any User instance.
I'm trying to figure out how Whenever gem is working.
It works by generating a crontab file with your commands. Then OS/crond take over.
Then I start my localhost and nothing happens, nothing in the log and it doesn't create any User instance.
Well, that's not a valid rake command you have there. Check cron logs, you'll see an error there. Did you mean to use runner?
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.
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 :)
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
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?