I'm trying to set up whenever gem, but I can't. schedule.rb file:
set :output, "#{path}/log/cron-whenever.log"
every '1 * * * *' do
command "#{path}/sh_files/db_backup.sh"
end
Setting up:
~# whenever --update-crontab everything
[write] crontab file updated
~# crontab -l
# Begin Whenever generated tasks for: everything
1 * * * * /bin/bash -l -c '/some/path/to/files/db_backup.sh >> /some/path/to/files/cron-whenever.log 2>&1'
# End Whenever generated tasks for: everything
And after starting rails server and waiting... nothing happens. No logs update, do db_backup.sh update. Nothing... And I checked manually /some/path/to/files/db_backup.sh command, it works...
It should be schedule.rb instead of scheduler.rb according to the Whenever gem
Related
I have set up a couple of rake tasks to run automatically with the whenever gem . The tasks involve running a background Job, performing a database Blazer check and sending failing checks emails.
The last two are provided by the Blazer gem.
This is my schedule.rb file:
set :environment, "development"
every 1.minutes do
rake "health_check:perform"
end
every 1.minutes do
rake 'blazer:run_checks'
end
every 2.minutes do
rake "blazer:send_failing_checks"
end
The "health_check:perform" rake task always performs as expected, and I can see the Job being run in my server log every minute.
The other two, however, don't seem to be executing at all.
I can tell blazer:run_checks is not executing because when a Blazer check is run either manually or automatically, the blazer_checks table last_run_at column value is updated with the timestamp when the last check was run. Also, I can see the update DB transaction in my server log when that happens.
On the other hand "blazer:send_failing_checks" is not working either because well, no emails are ever delivered.
However, if I manually invoke the task from my console via rake "blazer:run_checks" or rake "blazer:send_failing_checks" they perform normally (blazer_checks table is updated and failing check emails are delivered).
I can't spot any error messages in my server log, altough I believe by default rake tasks are set to run in silent mode? Since running whenever -l to list my crontab file returns something like:
* * * * * /bin/bash -l -c 'cd /home/user/code/myapp && RAILS_ENV=development bundle exec rake health_check:perform --silent'
* * * * * /bin/bash -l -c 'cd /home/user/code/myapp && RAILS_ENV=development bundle exec rake blazer:run_checks --silent'
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/user/code/myapp && RAILS_ENV=development bundle exec rake blazer:send_failing_checks --silent'
Any ideas? How can I debug this?
I am using the whenever gem to create the rake task, i am expecting to get a line output "Teaching Thabo how to schedule tasks" on my terminal every 2 minutes, i am new to Rails, here is the code for my rake task file which i have named request_invoice.rake
namespace :request_invoice do
desc "Teaching Thabo how to schedule tasks"
task test_tasking: :environment do
puts "Learning the tasking in rails..."
end
end
And the whenever gem created a schedule.rb file in the config of my project, i have the following code in the file
every 2.minutes do
rake 'request_invoice:test_tasking'
end
I have ran the whenever command on the terminal, it gives the following:
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,5 2,54,56,58 /bin/bash -l -c 'cd /home/sable/GMS/grading- management-solution && RAILS_ENV=production bundle exec rake request_invoice:test_tasking --silent'
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.
Did you run the:
whenever --update-crontab
command? According to the gem's page, this is required for your crontab file to be updated.
First, you need to check whether you have added tasks to crontab or not using:
crontab -e
If you do not see your task, then add it using:
whenever -i
You can track your cron jobs by setting a log file. Just add the following to the top of config/schedule.rb:
set :output, "log/cron_log.log"
Now, you should be able to check your cron logs (if tail is available to you then do the following from the root of your application path):
tail -f log/cron_log.log
I am trying to make a simple cron job running this task:
Here is my schedule.rb:
set :environment, "staging"
set :path, "/var/www/my_app/current"
every 2.minutes do
# specify the task name as a string
rake 'cron_test'
end
Here is the task:
task :cron_test => :environment do
out_file=File.new("cron_test.txt", "w")
out_file.puts(Time.now.to_s)
out_file.close
end
I tried to do what is advised on this page but nothing works:
Cron job not working in Whenever gem
When I run crontab -l :
# Begin Whenever generated tasks for: /var/www/my_app/current/config/schedule.rb
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 /var/www/my_app/current && RAILS_ENV=staging bundle exec rake cron_test'
# End Whenever generated tasks for: /var/www/my_app/current/config/schedule.rb
When I run grep CRON /var/log/syslog:
Jun 29 17:22:01 my_server CRON[21164]: (root) CMD (/bin/bash -l -c 'cd /var/www/my_app/current && RAILS_ENV=staging bundle exec rake cron_test')
Any ideas?
Thanks
What happens when you manually run the command from crontab on the console? Try to run in. See if there are any errors.
/bin/bash -l -c 'cd /var/www/my_app/current && RAILS_ENV=staging bundle exec rake cron_test'
Might be something with local or cron environment. Also logs would be great, capturing any running errors. You can set output log to capture any problems with crontab running the command.
set :output, '/path/to/file.log'
for you example:
set :output, '/var/log/my_app.cron.log'
set :environment, "staging"
set :path, "/var/www/my_app/current"
every 2.minutes do
# specify the task name as a string
rake 'cron_test'
end
Here's the documentation whenever output redirection
Thanks it is working now!
When I created the /var/log/staging_cron.log I read this:
/bin/bash: bundle: command not found
I added env :PATH, ENV['PATH'] on the first line of my schedule.rb, updated the crontab (whenever --update-crontab) and now my test file is well created and updated.
I follow the railscast whenever by javan tutorial but I seems to unable to make it happen.
Is there a MUST to install capistrano to have cron job? it is because I don't have the deploy.rb. I list down the steps in details. If there any steps I had missed to cause my cron not working? I always get a report say I got a new mail but I didnt send any email operation.
Step1:
wheneverize .
====> schedule.rb in config folder
Step 2:
[Scheduler.rb]
every '1 * * * *' do
runner 'Company.count'
end
Step 3: [what is "store"?]
whenever --update-crontab store
Step 4: [config/deploy.rb]<-- I don't have deploy files so I create it myself.
after "deploy:symlink", "deploy:update_crontab"
namespace :deploy do
desc "Update the crontab file"
task :update_crontab, :roles => :db do
run "cd #{release_path} && whenever --update-crontab #{application}"
end
end
Step 5 :
whenever --update-crontab store
crontab -l
What have I missed? why it didnt work? Please enlighten me step by step because I am newbie in ROR.. thanks.
The Whenever docs have an example of deploying Whenever with Capistrano. Just add the following to the top of your Capistrano deploy config.
set :whenever_command, "bundle exec whenever" # set this first if using bundler
require "whenever/capistrano"
Then Whenever will install into cron as part of the deploy.
UPDATE
To test that Whenever was successfully updated cron, ssh to the target machine as the Capistrano deploy user for the target machine and run crontab -l. You should see output similar to this:
crontab -l
# Begin Whenever generated tasks for: app_name
0 0 * * * /bin/bash -l -c 'cd /opt/path/app_name/releases/20120321133343 && RAILS_ENV=production rake group:task --silent'
0 3 * * 6 /bin/bash -l -c '/opt/path/app_name/shared/bin/script'
# End Whenever generated tasks for: app_name
I have an application that contains a bunch of tasks, and every day I want to run a cron job that creates a DayTask for each Task in the database. A Task has_many DayTasks and these daytasks are what users will be checking off every day. I'm using the whenever gem but it doesn't seem to be running at all. Any ideas?
config/schedule.rb
every 1.day, :at => "12:01am" do
runner "Task.generate_tasks_for_day"
end
Task.rb
def generate_tasks_for_day
Task.all.each do |task|
task.day_tasks.create(:target_date => Date.today)
end
end
result of running the 'whenever command'
1 0 * * * /bin/bash -l -c 'cd /home/grant/rails_projects/GoalTwist && script/rails runner -e production '\''Task.generate_tasks_for_day'\'''
Note: I've been changing the times in config/schedule.rb every time I want to test run it.
Finally I have solved how to run the gem Whenever.
It's working good on production, but not in development mode (I think that to working good in dev mode you must do some tricks).
Then, these are the processes to do:
install the gem
write your scheduler.rb file
push to the remote server
login to the remote server (for example with ssh)
see if whenever is good uploaded by running in terminal: whenever
update whenever crontab by running: whenever --update-crontab
restart the server crontab (for example in Ubuntu server): sudo service cron restart
check if crontab is good implemented on the server: crontab -l
That's it!
Personally, I prefer to set up my crons directly from the server:
Edit the crontab: crontab -e
Append my cron (e.g. every day at 5:00 AM - can be little different for not-Linux-based server):
0 5 * * * /bin/bash -l -c 'cd /path_to_my_app/current && RAILS_ENV=production bundle exec rake my_cron_rake'
Check if good implemented: crontab -l
Done
You have to actually register the job with the crontab by running:
whenever --update-crontab
Also if you're trying to get jobs running locally, add
:environment => "development" to your task
runner "MyTask.some_action", :environment => "development"
Try to execute the whenever generated command directly from terminal or add the following line
MAILTO=your#emailadress.com
to your crontab.
if whenever command gives you -bash: whenever: command not found, try bundle exec whenever