I'm using the latest version of the whenever gem with Rails 3.1.1 for cron tasks. After I used the whenever command on the terminal
whenever -c
to clear the crontab, whenever I type in
crontab -l
whereas it used to say something like "there are no cron tasks" (this is not verbatim) now it just displays a blank space about the size of two empty lines. Also if I have a cron task setup and I type the same command into the terminal again, those two empty lines come up before it shows the cron tasks. I'm sorry if this is a minor issue. Everything appears to be working fine but I just want to make sure I didn't screw anything up that'll come back to haunt me somewhere down the line. Thanks!
You need to change the task in the schedule.rb file which is generated by whenever gem.
After changing your cron task in the schedule.rb you have to update your crontab file and you can use this command to do that :-
whenever --update-crontab f(here f is your application name)
crontab -l is used to see your current crontab file.
Hope it helps
Related
I have a Rails runner task that I want to run from cron, but of course cron runs as root and so the environment is set up improperly to get RVM to work properly. I've tried a number of things and none have worked thus far. The crontab entry is:
* 0 * * * root cd /home/deploy/rails_apps/supercharger/current/ && /usr/local/rvm/wrappers/ruby-1.9.3-p484/ruby bundle exec rails runner -e production "Charger.start"
Apologies for the super long command line. Anyhow, the error I'm getting from this is:
ruby: No such file or directory -- bundle (LoadError)
So ruby is being found in the RVM directory, but again, the environment is wrong.
I tried rvm alias delete [alias_name] and it seemed to do something, but darn if I know where the wrapper it generated went. I looked in /usr/local/rvm/wrappers and didn't see one with the name I had specified.
This seems like a common problem -- common enough that the whenever gem exists. The runner command I'm using is so simple, it seemed like a slam dunk to just put this entry in the crontab and go, but not so much...
Any help with this is appreciated.
It sounds like you could use a third-party tool to tether your Rails app to cron: Whenever. You already know about it, but it seems you never tried it. This gem includes a simple DSL that could be applied in your case like:
every :day # Or specify another period, or something else, see README
runner "Charger.start"
end
Once you've defined your schedule, you'll need to write it into crontab with whenever command line utility. See README file and whenever --help for details.
It should not cause any performance impact at runtime since all it does is conversion into crontab format upon deployment or explicit command. It's not needed, once the server is running, everything is done by cron after that.
If you don't want an extra gem anyway, you might as well check what command does it issue for executing your task. Still, an automated way of adding a cron task is easier to maintain and to deploy. Sure, just tossing a line into the crontab is easier — just for you and just this once. Then it starts to get repetitive and tiring, not to mention confusion for other potential developers who will have to set up something similar on their own machines.
You can run cron as different user than root. Even in your example the task begins with
* 0 * * * root cd
root is the user that runs the command. You can edit it with crontab -e -u username.
If you insist on running cron task as root or running as other user does not work for some reason, you can switch user with su. For example:
su - username -c "bundle exec rails runner -e production "Charger.start"
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
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
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
I am using whenever gem with rails 3. On my production server (ubuntu) , the runner task does not run. I tried setting the :set job_template to get -l -i as mentioned in this github ticket. However that does not solve the problem.
The problem on this particular production ubuntu is that the ruby path is not there in echo $PATH:
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Whereas the ruby path is /var/rails/myapp/shared/bundle/ruby/1.8/bin
So if I manually edit the crontab file and add PATH=/var/rails/myapp/shared/bundle/ruby/1.8/bin:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games to the crontab file, the runner task is executed correctly.
However every time I do a deploy, I need to manually edit the crontab file to add the PATH statement to it.
Is there any way in whenever to add this PATH line in crontab file, so that there would not be any need to do this manually after every deploy?
Thanks
I am not using RVM and adding the below code in the file config/schedule.rb(place where u write whenever gem related code) worked for me.
env :PATH, ENV['PATH']
I think if you add /var/rails/myapp/shared/bundle/ruby/1.8/bin to the PATH of whatever user cron is running under on the server, it should be picked up. Or, you could add it in the whenever schedule.rb:
env :PATH, "$PATH:/var/rails/myapp/shared/bundle/ruby/1.8/bin"
That should do the trick, but I haven't tested it.
The answer from idlefingers looks mostly correct, but based on the comment from ami, I would change that line to the following:
env :PATH, "#{ENV["PATH"]}:/var/rails/myapp/shared/bundle/ruby/1.8/bin"
Notice the string interpolation for the environment key for PATH. I have not tested this, but based on ami's comment, this should fully expand the path string in the crontab file as expected.
Add the PATH statement to the top of the crontab file, before the line that starts
# BEGIN Whenever generated tasks for:
and you shouldn't have to manually edit your crontab file every time