'I have set up a cron with wehenever, but its not working. I tried to run the command manually and i get the error /bin/bash: bin/rails: Permission denied.
Here what the command of the cron looks like:
/bin/bash -l -c 'cd /var/www/domain.net/main && bin/rails runner -e production '\''User.weekly_update'\'''
I also tried to run this command as root but i got the same message.
Try to make bin/rails executable:
chmod u+x bin/rails
This is, of course, assuming that bin/rails is owned by the crontab's user.
I found that use of RVM can complicate this. A worthy alternative is to make your whenever job into a rake job instead of a runner job:
every 7.days do
rake "user:weekly_update"
end
This does, of course, necessitate an extra bit of code in your lib/tasks directory:
namespace :user do
task :weekly_update=> :environment do
User.weekly_update
end
end
i had the same problem and solved this as follows:
(iam work with rvm and my */bin/rails has already +rx privileges)
As you can see in whenever-github you can change job_type within config/schedule.rb
job_type :runner, "cd :path && /other-path/path-x/bin/rails runner -e :environment ':task' :output"
Related
I'm using whenever gem for cronjobs in rails application - production.
I'm getting an error bundler: not executable: bin/rails
scheduler.rb
every 15.minute do
runner 'TestJob.perform_later()'
end
crontab
0,15,30,45 * * * * /bin/bash -l -c 'cd /home/deploy/my-app/releases/20190719103116 && bundle exec bin/rails runner -e production '\''TestJob.perform_later()'\'''
but when i run /bin/bash -l -c 'cd /home/deploy/my-app/releases/20190719103116 && bundle exec bin/rails runner -e production '\''TestJob.perform_later()'\''' in my bash replacing bin/rails with just rails this work fine.How to fix this?
Try running it via a rake task:
config/schedule.rb
every 15.minutes do
rake 'testing:run_tests'
end
lib/tasks/testing.rake
namespace :testing do
desc 'Run tests'
task run_tests: :environment do
TestJob.perform_later
end
end
Quick Answer
With the intent of still using the original runner job type, simply add this to your schedule.rb:
set :runner_command, "rails runner"
Long Answer
The documentation isn't so clear about it, but you can see the default runner job type being set as such in Whenever's /lib/whenever/setup.rb:
job_type :runner, "cd :path && :bundle_command :runner_command -e :environment ':task' :output"
In that same file you can see that this :runner_command setting is set with:
set :runner_command, case
when Whenever.bin_rails?
"bin/rails runner"
when Whenever.script_rails?
"script/rails runner"
else
"script/runner"
end
So by default your cron command will be created using bin/rails runner unless you overide it with the above answer.
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 have installed whenever gem:
I want to clean the directory public/uploads/tmp in my app ruby on rails 3.1 each 5 minutes.
every 5.minutes do
#here go the code for clean the directory tmp
end
How can I do it?
Thank you!
You could try using FileUtils#rm_rf included in the standard library. For example:
FileUtils.rm_rf Dir.glob("#{Rails.root}/public/uploads/tmp/*")
Edit (to use it with whenever gem)
An approach by using a rake task could be:
1) Create a rake task in f.ex: lib/tasks/cleanup.rake with something similar to the following:
require 'fileutils'
namespace :app do
desc "Cleanup temp uploads"
task :cleanup => :environment do
FileUtils.rm_rf Dir.glob("#{Rails.root}/public/uploads/tmp/*")
end
end
2) In config/schedule.rb (created by whenever after running the wheneverize command):
every 5.minutes do
# run the previous app:cleanup task
rake "app:cleanup"
end
3) Whenever is only a wrapper to easily define crontab jobs, so now we need to export the defined schedule into the crontab file for the current user. To do that we should type from the application root:
bundle exec whenever -w
4) You can check that it worked by typing crontab -l and you should the something like the following:
# Begin Whenever generated tasks for: /tmp/whene/config/schedule.rb
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /tmp/whene && RAILS_ENV=production bundle exec rake app:cleanup --silent
As a side note, if you want the operation to write some log output, please check this page on the whenever github wiki.
Hope it helps.
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