Cannot get whenever to run my rake task - ruby-on-rails

I am trying to get whenever to run my rake task but it doesn't work.
#Command Line
$ whenever -w
[write] crontab file written
#schedule.rb
every :year, :at => "2014-07-25 17:39:48 -0700" do
rake 'timeperiod:create_timeperiod'
end
The rake command saves a model in the db and puts text so I would know if it worked (which it does when I run rake). Is there syntactically something wrong with what I did?
Note, the time and date in schedule.rb is arbitrary, I keep changing it to two minutes from now before testing.

Your task is probably failing because the shell used by cron is not the same you use with your normal user.
Check the log of the crontab:
grep CRON /var/log/syslog

Why don't you verify that your command is getting added to the crontab?
$ crontab -l
You can also add the job manually.
$ whenever
* * * * * * bash -l 'cd /path/to/dir; rake your:task'
$ crontab -e
# add the cron job
Also, output from cron jobs doesn't output to the screen; it goes to your mail. Check your mailbox.
From the man page:
When executing commands, any output is mailed to the owner of the
crontab (or to the user named in the MAILTO environment variable in
the crontab, if such exists). The children copies of cron running
these processes have their name coerced to uppercase, as will be seen
in the syslog and ps output.

For me, I'm using command but I think it's not the best case. Says my repo is in $HOME/www/virtualspirit, in my repository. So my whenever command will be cd /www/virtualspirit && /$HOME/.rvm/rubies/ruby-2.1.2/bin/rake timeperiod:create_timeperiod RAILS_ENV=production
double check it in crontab -e and see whether it saved.

There were a couple issues here.
The best way to add cron jobs to whenever is with the command whenever --update-crontab <name of identifier>
The format was incorrect. By specifying the time in cron directly, I was able to make it work.
For example:
#schedule.rb
every '13 15 26 7 *' do
rake 'timeperiod:create_timeperiod'
end

Related

Ruby on Rails - Whenever Gem + Cron Not Running

TL;DR: My Whenever Gem scheduled task does not run at all automatically but will manually.
I posted about this earlier in the week but got no responses, I'm attempting to implement the Whenever gem into my RoR 5 application. Once a year I want it to run a method in my 'User' model, but for testing purposes I have made it once every minute, like so:
Schedule.rb:
set :output, "/home/ubuntu/workspace/log/cron.log"
set :environment, 'development'
every 1.minute do
runner "User.(methodhere)"
end
User.rb:
def self.(methodhere)
User.all.each do |user|
user.update(remaining_days: user.total_days)
end
end
In multiple places I have read that sometimes cronjobs dont run properly in development mode, so I jumped through all of the hoops to put my application into production mode, and that did not help.
I then found that you can manually run these jobs in the command line, which I then tried to do using the command found doing:
whenever --update-cron
then
crontab -l
which showed
# Begin Whenever generated tasks for:
* * * * * /bin/bash -l -c 'cd /home/ubuntu/workspace && bundle exec
bin/rails runner -e development '\''User.new.(methodhere)'\'' >>
/home/ubuntu/workspace/log/cron.log 2>&1'
Running this manually:
/bin/bash -l -c 'cd /home/ubuntu/workspace && bundle exec
bin/rails runner -e development '\''User.(methodhere)'\'' >>
/home/ubuntu/workspace/log/cron.log 2>&1'
makes it work, and executes the (methodhere). Whenever just does not make it run automatically at the set interval.
Another thing I found was to try and restart cron, through cron restart, but I am receiving:
cron: can't open or create /var/run/crond.pid: Permission denied
I'm not sure if that has anything to do with the IDE I'm using, Cloud 9.
Many google searches have left me with nothing.
NOTE: I'm a very new developer with RoR, so any guidance on the matter would be greatly appreciated, thanks!

How to put filename in cron job (rails)?

I am using whenever gem. I am calling runner to run job. I called runner for the same job by 3 type. I am not sure which one working here.
I used command whenever --update-crontab project_name
then crontab -l
Schedule.rb
set :output, 'log/whenever.log'
every 1.days , :at => '03:51 pm' do
runner "SomeJob.perform_later", filename: '/app/jobs/some_job.rb'
end
every 1.days , :at => '03:51 pm' do
runner "SomeJob.perform_later", filename: './app/jobs/some_job.rb'
end
every 1.days , :at => '03:51 pm' do
runner "SomeJob.perform_later"
end
Also these ran only once. I am having difficult time to debug here.
Can anyone tell which one is correct? Also what is the correct way to debug in this scenario?
Running crontab -l gives me this -
51,51,51 16 * * * /bin/bash -l -c 'cd /home/rahul/orthoweb && bin/rails runner -e production '\''InvestigationStopJob.perform_later'\'' >> log/whenever.log 2>&1
To run in development environment I ran this command -
whenever --update-crontab --set environment='development'
But this gives me this message -
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
There are some issues with running the jobs in development mode.
What works is that you have to close all tabs before running your job and scheduler.
There is no need to change setup of your env in schedule file.
To run in development use this
whenever --update-crontab --set environment='development'
Without specifying filename jobs does not run properly. The following line works correctly.
every 1.days , :at => '03:51 pm' do
runner "SomeJob.perform_later", filename: '/app/jobs/some_job.rb'
end
I think, that the resulting crontab contains only one of those tasks, because they were overwritten.
To be sure, check out the resulting crontab by typing whenever. I think that there will be only one entrance.

Rails: Cron jobs with whenever gem

I'm new to cron job as well as rails. I have installed whenever gem. Now I want to delete a file from tmp/cache/foo.txt.
schedule.rb
every 5.minutes do
command "rm '#{path}/tmp/cache/foo.txt' "
end
And when i run whenever command it says:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'rm '\''/home/techbirds/shivam/tmp/cache/foo.txt'\'''
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options
So how to do it from starting?
You should update crontab (configuration file that defines commands to run periodically on a desired schedule) according to you new schedule.rb rules, so you should run the following command (if this job is set to a specific environment in your schedule.rb you should also --set environment):
whenever --update-crontab
To view your current jobs use crontab -e (jobs generated by whenever would be enclosed in # Begin Whenever and # End Whenever blocks).

Scheduling rake task with cron

I'm trying to set daily cron job to update my site stats, but it looks like it doesn't work.
Cron entry (for deployer user):
0 0 * * * cd /var/www/my_site/current && rake RAILS_ENV=production stats:update
I'm running ubuntu server, with rbenv.
Any idea what's wrong?
Many times $PATH is defined differently when cron runs compared to when you are working in your own shell. Do "whereis rake" to find the full path to rake and then replace "rake" with its full path. (I am assuming that the "cd" command is working, so I am focusing on whether "rake" is found / running properly.)
Has cron sent you any emails with error messages after you added your command to your crontab?
You might want to run "crontab -l" under the proper user account to make sure that your cron command is actually registered within the crontab, especially if you aren't receiving any emails.
The presence of a Gemfile can also affect the ability to properly run rake. See, for example, Error: "Could not find rake", yet Rake is installed

How to stop cron jobs created by "whenever" gem

I'm using the "whenever" gem and got it working by doing:
whenever --set environment=production --update-crontab theCronJob
The interval I'm using is 2 minutes since I'm still trying to figure it out. However, now I get a You have mail message in my terminal window every 2 minutes. I guess the cron runs and lets me know about it. How do I stop my cron from running? These messages are starting to pile up.
Thank you
To delete the auto-generated cronjobs from your crontab, run whenever against your defintion file with the -c flag:
$ whenever -c theCronJob
Alternatively, open your crontab...
$ crontab -e
... and then manually delete the undesired entries.
following will delete the scheduled crontab:
crontab -r

Resources