cron rake在ror下的使用whenever gem进行简化
https://github.com/javan/whenever
whenever enhanced cron on rails way
初始
Getting started $ cd /my/rails/app
$ wheneverize .
生成cron代码
The whenever command $ cd /my/rails/app
$ whenever
This will simply show you your schedule.rb file converted to cron syntax. It does not read or write your crontab file. Run whenever --help for a complete list of options.
部署
将部署代码集成进capistrano
set :whenever_command, "bundle exec whenever" require "whenever/capistrano"
放到最后,因为至少要先安装上whenever gem first
其它重要的操作
rails 使用 whenever
rails whenever使用
参考:
https://github.com/javan/whenever 作者:javan javan / whenever
http://myrev.javaeye.com/blog/830164 作者:myrev Rails3利用whenever gem调度发送邮件
http://asciicasts.com/episodes/164-cron-in-ruby Cron in Ruby
whenever用于提供用户的定时任务,在linux下写crontab
1. 安装
$ gem install whenever
或在Gemfile中添加
gem 'whenever', :require => false
2. 在项目目录下
$ cd ~/workspace/deploy
$ wheneverize .
输出:
[add] writing `./config/schedule.rb'
[done] wheneverized!
即创建 config/schedule.rb配置文件
3. 在schedule.rb中写入自己需要定时运行的任务:
如:
every 1.day do
rake "log:clear"
end
4. 运行whenever命令,查看生成的crontab命令
$ whenever
=> 0 0 * * * /bin/bash -l -c 'cd /home/user01/workspace/deploy && RAILS_ENV=production rake log:clear --silent'
可以看到默认环境为生产环境
5. 写入到crontab中:
$ whenever -s environment=development -w /home/user01/workspace/deploy/config/schedule.rb
=> [write] crontab file written
其中environment后为项目运行环境,-w后为schedule.rb的默认路径
6. 更新crontab
$ whenever -i
7. 查看crontab
$ crontab -l
=>
Begin Whenever generated tasks for: /home/user01/workspace/deploy/config/schedule.rb 0 0 * * * /bin/bash -l -c 'cd /home/user01/workspace/deploy && RAILS_ENV=production rake log:clear --silent' # End Whenever generated tasks for: /home/user01/workspace/deploy/config/schedule.rb
8.清除crontab为:
$ whenever -c
other info
Setting variables on the fly
If you wish to override variables in your schedule.rb file at runtime you can do so using the --set option. This is especially useful for changing your environment when deploying to a staging server or something similar.
Example:
whenever --set environment=staging
You can set more than one variable by forming a query string. Make sure to use quotes.
whenever --set 'environment=staging&cron_log=/my/other/log.txt'
A couple of notes:
Be aware of escaping and/or quoting when using --set with multiple key/value pairs (in other words, be careful with the “&”). Use --set cron_log=PATH from the command-line to override any set :output, PATH calls in your schedule.rb ( --set output=PATH DOES NOT work).So you can define different tasks per environment:
case @environment when 'production' every 1.day, :at => "#{Time.parse('12:00 A').getlocal.strftime("%H:%M")}" do runner "Company.send_later(:create_daily_stories!)" end when 'staging' every 15.minutes do command "thinking_sphinx_searchd reindex" end end
In some situations, you must set the environment with a separate bash command, as seen in this cap task:
desc "Update the crontab file" task :update_crontab, :roles => :db do run "cd #{release_path}; whenever --set environment=staging; whenever --update-crontab #{application}" end
启用cron 日志
2009 - 08 - 21
Ubuntu下Cron Log的启用设置
文章分类: 操作系统
原文
调试Cron脚本的时候遇到了一些问题,当我去查看Cron Log文件时竟然发现不存在,我很震惊。
默认情况下,Ubuntu Linux不启用Cron Log。
下面的几个步骤启用Cron Log让我们更好的调试Cron:
编辑 /etc/syslog.conf,并且打开以cron.*开始的那行注释。 运行 /etc/init.d/sysklogd restart 。 运行 /etc/init.d/cron restart 。
即可。
以上错误,
0 down vote
By default the cron log in ubuntu is located at /var/log/syslog. Use the below command to check the cron entries in this file.
grep cron /var/log/syslog
Advanced Crontab
The Crontabs discussed above are user crontabs. Each of the above crontabs is associated with a user, even the system crontab which is associated with the root user. There are two other types of crontab.
Firstly, as mentioned above anacron uses the run-parts command and /etc/cron.hourly , /etc/cron.weekly , and /etc/cron.monthly directories. However anacron itself is invoked from the /etc/crontab file. This file could be used for other cron commands, but probably shouldn't be. Here's an example line from a ficticious /etc/crontab :
00 01 * * * rusty /home/rusty/rusty-list-files.sh
This would run Rusty's command script as user rusty from his home directory. However, it is not usual to add commands to this file. While an experienced user should know about it, it is not recommended that you add anything to /etc/crontab . Apart from anything else, this could cause problem if the /etc/crontab file is affected by updates! Rusty could lose his command.
The second type of crontab is to be found in /etc/cron.d . Within the directory are small named crontabs. The directory is often used by packages, and the small crontabs allows a user to be associated with the commands in them.
Instead of adding a line to /etc/crontab which Rusty knows is not a good idea, Rusty might well add a file to /etc/cron.d with the name rusty , containing his cron line above. This would not be affected by updates but is a well known location.
When would you use these alternate crontab locations? Well, on a single user machine or a shared machine such as a school or college server, a user crontab would be the way to go. But in a large IT department, where several people might look after a server, then /etc/cron.d is probably the best place to install crontabs - it's a central point and saves searching for them!
You may not need to look at /etc/crontab or /etc/cron.d , let alone edit them by hand. But an experienced user should perhaps know about them and that the packages that he/she installs may use these locations for their crontabs.
Special strings
Cron also offers some special strings:
string
meaning
@reboot
Run once, at startup.
@yearly
Run once a year, "0 0 1 1 *".
@annually
(same as @yearly)
@monthly
Run once a month, "0 0 1 * *".
@weekly
Run once a week, "0 0 * * 0".
@daily
Run once a day, "0 0 * * *".
@midnight
(same as @daily)
@hourly
Run once an hour, "0 * * * *".
Usage: "@reboot /path/to/execuable1" will execute /path/to/executable1 when the system starts. See "man 5 crontab" for more info.
cron howto
https://help.ubuntu.com/community/CronHowto
all relevent information is included in http://blog.scoutapp.com/articles/2010/09/07/rvm-and-cron-in-production
https://github.com/javan/whenever/issues/issue/88
echo $GEM_PATH
/home/mlzboy/.rvm/gems/ruby-1.9.2-p0:/home/mlzboy/.rvm/gems/ruby-1.9.2-p0@global
It seems this is what RVM looks for and is missing because cron does not load your home environment.
Ref: http://adminschoice.com/crontab-quick-reference
You can see what RVM has set the GEM_PATH to by running:
echo $GEM_PATH
http://wiki.joyent.com/smartmachine:rails:cron
http://blog.scoutapp.com/articles/2010/09/07/rvm-and-cron-in-production
http://hectcastro.me/post/722196371/rvm-and-cron
Trading Fish
My name is Hector Castro and I am software developer at Wharton Research Data Services , located in Philadelphia, PA. Check out the RSS feeds I monitor, my GitHub account, and an archive of my blog entries .
You can get in contact with me via Twitter or E-mail .
ARCHIVE / RSS2010-06-21
RVM and Cron
Recently I found myself having to execute a cronjob using specific rubies and gemsets contained within RVM . The following snippet is what I used as a solution:
@hourly bash - l - c 'rvm use rbx@gemset && rake cron'
The - l forces bash to act as if it had been invoked as a login shell. The - c tells it to read the string that follows.
查看更多关于cron rake在ror下的使用whenever gem进行简化的详细内容...