诗歌rails之关于ruby script/runner

    Rails开发的时候,估计console和Debug我们会比较多用,JE上搜Script/runner没有什么相关的资料。

    runner和console一样是内置在script下的命令,也非常类似的提供console的功能,只不过是console针对的是命令行,runner针对的是文件。换句话说,都是提供Rails执行环境。

    runner最大可能的应用,在于定时执行。例如,你的Rake任务,对于一些没有满足条件的用户发送邮件通知,每晚执行:

    我们的需求是,每天晚上,用户不怎么访问的时候检查系统日志,如果,超过范围就清理日志:
1. 在lib下建立文件rake任务clear_daemon_log

Ruby代码
  1. namespace :log do  
  2.   desc "Truncates all *.log files in log/ to zero bytes"  
  3.   task :clear_all do  
  4.     FileList["#{RAILS_ROOT}/log/*.out"].each do |log_file|  
  5.       f = File.open(log_file, "w")  
  6.       f.close  
  7.     end  
  8.     FileList["#{RAILS_ROOT}/log/*.err"].each do |log_file|  
  9.       f = File.open(log_file, "w")  
  10.       f.close  
  11.     end  
  12.     FileList["#{RAILS_ROOT}/log/*.log"].each do |log_file|  
  13.       f = File.open(log_file, "w")  
  14.       f.close  
  15.     end  
  16.   end  
  17. end  
namespace :log do
  desc "Truncates all *.log files in log/ to zero bytes"
  task :clear_all do
    FileList["#{RAILS_ROOT}/log/*.out"].each do |log_file|
      f = File.open(log_file, "w")
      f.close
    end
    FileList["#{RAILS_ROOT}/log/*.err"].each do |log_file|
      f = File.open(log_file, "w")
      f.close
    end
    FileList["#{RAILS_ROOT}/log/*.log"].each do |log_file|
      f = File.open(log_file, "w")
      f.close
    end
  end
end



2. 在script下建立文件logclean

Ruby代码
  1. #调用上面的Rake任务  
  2.   
  3. require 'rake'  
  4. Rake.application.rake_require 'http://www.cnblogs.com/lib/tasks/clear_daemon_log'  
  5. Rake.application['log:clear_all'].invoke  
#调用上面的Rake任务

require 'rake'
Rake.application.rake_require 'http://www.cnblogs.com/lib/tasks/clear_daemon_log'
Rake.application['log:clear_all'].invoke


3. 如下调用

Ruby代码
  1. 0 1,13 * * * RAILS_ENV=production /.../current/script/runner /.../current/script/dbcleaner  
0 1,13 * * * RAILS_ENV=production /.../current/script/runner /.../current/script/dbcleaner


4. 查看cron

Ruby代码
  1. crontab -l -u user  
crontab -l -u user


5. 通过spec生成rpm

Ruby代码
  1. %prep  
  2. echo Building %{name}-%{version}-%{release}  
  3.   
  4. # Since we'll be copying the files from the repository checkout instead of  
  5. # extracting a tarball (-T), we only need to create the target directory (-c).  
  6. %setup -q -T -c  
  7.   
  8. %build  
  9.   
  10. %clean  
  11. #rm -rf %{buildroot}  
  12.   
  13. %install  
  14. rm -rf %{buildroot}  
  15. mkdir -p %{buildroot}/var/spool/cron  
  16. cp %{SOURCE0} %{buildroot}/var/spool/cron/××  
%prep
echo Building %{name}-%{version}-%{release}

# Since we'll be copying the files from the repository checkout instead of
# extracting a tarball (-T), we only need to create the target directory (-c).
%setup -q -T -c

%build

%clean
#rm -rf %{buildroot}

%install
rm -rf %{buildroot}
mkdir -p %{buildroot}/var/spool/cron
cp %{SOURCE0} %{buildroot}/var/spool/cron/××



这样在系统安装的时候,就可以执行这个cron了

转载于:https://www.cnblogs.com/orez88/articles/1553051.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值