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?
Related
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, 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 am doing a cron tab in AWS - Elastic Beanstalk with Ruby on Rails 3, but I don't know what is wrong.
I have this code in my .ebextensions/default.config
container_commands:
01remove_old_cron_jobs:
command: "crontab -r || exit 0"
02send_test_email:
command: crontab */2 * * * * rake send_email:test
leader_only: true
I receive this error:
Failed on instance with return code: 1 Output: Error occurred during build: Command 02send_test_email failed .
UPDATE 1
I tried next:
crontab.txt
*/2 * * * * rake send_email:test > /dev/null 2>&1
default.config
02_crontab:
command: "cat .ebextensions/crontab.txt | crontab"
leader_only: true
RESULT: No errors, but it does not work.
UPDATE 2
crontab.sh
crontab -l > /tmp/cronjob
#CRONJOB RULES
echo "*/2 * * * * /usr/bin/wget http://localhost/crontabs/send_test_email > /dev/null 2>&1" >> /tmp/cronjob
#echo "*/2 * * * * rake send_email:test > /dev/null 2>&1" >> /tmp/cronjob
crontab /tmp/cronjob
rm /tmp/cronjob
echo 'Script successful executed, crontab updated.'
default.config
02_crontab:
command: "/bin/bash .ebextensions/crontab.sh"
leader_only: true
RESULT: Works with url, but not with rake task.
Updated for 2018
In order to get this to work on the latest version of Elastic Beanstalk, I added the following to my .ebextensions:
.ebextensions/0005_cron.config
files:
"/etc/cron.d/mycron":
mode: "000644"
owner: root
group: root
content: |
56 11 * * * root . /opt/elasticbeanstalk/support/envvars && cd /var/app/current && /opt/rubies/ruby-2.3.4/bin/bundle exec /opt/rubies/ruby-2.3.4/bin/rake send_email:test >> /var/app/current/log/cron.log 2>&1
commands:
remove_old_cron:
command: "rm -f /etc/cron.d/*.bak"
How I got there:
There are four main issues to confront when trying to cron a rake task in AWS EB:
The first hurdle is making sure all of your EB and Rails environment variables are loaded. I beat my head against the wall a while on this one, but then I discovered this AWS forum post (login may be required). Running . /opt/elasticbeanstalk/support/envvars loads all of your environment variables.
Then we need to make sure we cd into the current app directory using cd /var/app/current.
Next we need to know where to find the bundle and rake executables. They are not installed in the normal bin directories, but are located in a directory specific to your ruby version. To find out where your executables are located, ssh into your EB server (eb ssh) and then type the following:
$ cd /var/app/current
$ which bundle
/opt/rubies/ruby-2.3.4/bin/bundle
$ which rake
/opt/rubies/ruby-2.3.4/bin/rake
You could probably guess the directory based on your ruby version, but the above commands will let you know for sure. Based on the above, your can build your rake command as:
/opt/rubies/ruby-2.3.4/bin/bundle exec /opt/rubies/ruby-2.3.4/bin/rake send_email:test
NOTE: If you update your ruby version, you will likely need to update your cron config as well. This is a little brittle. I'd recommend making a note in your README on this. Trust me, six months from now, you will forget.
The fourth thing to consider is logging. I'd recommend logging to the same location as your other rails logs. We do this by tacking on >> /var/app/current/log/cron.log 2>&1 to the end of our command string.
Putting all of this together leads to a cron command string of:
. /opt/elasticbeanstalk/support/envvars && cd /var/app/current && /opt/rubies/ruby-2.3.4/bin/bundle exec /opt/rubies/ruby-2.3.4/bin/rake send_email:test >> /var/app/current/log/cron.log 2>&1
Finally, I referenced the latest AWS documentation to build an .ebextensions config file for my cron command. The result was the .ebextensions/0005_cron.config file displayed at the top of this answer.
I am having the same issue. Though I figured out that the reason that rake task doesn't run correctly on eb is because of RACK_ENV, RAILS_ENV and BUNDLE_WITHOUT
Defaults of eb:
RACK_ENV: production
RAILS_ENV: production
BUNDLE_WITHOUT: test:development
When the cron runs rake task, it runs in development mode, and gives gem not found error, as gems grouped in development are not installed.
you can see this by changing your cron a bitfrom:
*/2 * * * * rake send_email:test > /dev/null 2>&1
to:
*/2 * * * * cd /var/app/current/ && /usr/bin/bundle exec /usr/bin/rake send_email:test > /tmp/cron_log 2>&1
and then checking the /tmp/cron_log file
To know the location of bundle and rake, run
which bundle
which rake
I tried setting RAILS_ENV in command in cron, but that didn't work aswell
One quick fix is to set
BUNDLE_WITHOUT to null
EDIT:
Finally I got it to work,
.ebextensions/.config
files:
"/tmp/cron_jobs" :
mode: "000777"
content: |
1 10 * * * cd /var/app/current/ && RACK_ENV=production rake some:task >> /var/app/current/log/cron_log 2>&1
encoding: plain
container_commands:
01_delete_cron_jobs:
command: "crontab -r -u webapp || exit 0"
02_add_cron_jobs:
command: "crontab /tmp/cron_jobs -u webapp"
leader_only: true
option_settings:
- option_name: RAILS_ENV
value: production
- option_name: RACK_ENV
value: production
Notice the '-u webapp' when removing and adding cron, this will run this cron under user webapp. The above will also run in production mode. And the output will be dumped in log/cron_log file.
If the above wont work then adding 'Bundle exec' before 'rake some:task' might work.
I've seen these used with separate files in .ebextensions, such as:
02send_test_email:
command: "cat .ebextensions/crontab | crontab"
leader_only: true
I haven't gotten around to it yet, but I took note of this along the way. Let us know if this works.
This stackoverflow post has much more information
After Update 1/2:
Cron doesn't know where rake is. Your application runs from /var/app/current, and you need to be running bundle exec rake from that directory.
Elastic beanstalk is horrible with logging errors, to get this right, ssh to the machine and experiment until you have the commands right, then put this back into your cron script. You can even try and re-run some of the eb scripts as found in the logs, then reverse that into your ebextensions files.
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