【问题标题】:Running whenever cron in Windows每当 cron 在 Windows 中运行
【发布时间】:2011-10-27 04:44:40
【问题描述】:

cronjobs 的新手和类似的东西,以前从未这样做过,所以现在我尝试在 RailsCasts 上看到它之后,将它用作插件。 因此,我正在尝试为我的 Ruby 应用程序运行 cron 作业,但它似乎无法正常工作,可能是因为代码的原因,或者可能是因为我使用的是 Windows 7?

这是我的服务器模型的代码

def self.ping
 Server.all.each do |t|
     if t.name.serverUp?
        @response = 'Up'
      else
        @response = 'Down'
     end
self.update_attribute(:serverStatus, @response.to_s)
end
end


def serverUp?
 if system 'ping '+name.to_s+' -n 1  > nul'
   @response = 'Up'
        else
  @response = 'Down'
end
  self.update_attribute(:serverStatus, @response.to_s)
@response
end

这是我的 schedule.rb 文件中的代码,其中包含 cron 作业

#every 2.minutes do
#  runner "Server.last.name = 'Works'"
#  runner "for x in Server.all.each {x.serverUp?}", environment =>"development"
#end

every 2.minutes do
  runner "Server.ping", environment =>"development"
end

我尝试了这两种方法,(我最近使用的是后者,我在服务器模型中创建了self.ping 方法。

所以当我在我的 rails 目录中运行 whenever 时,我得到的输出如下:

PS C:\SIS> whenever
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 C:/SIS
&& script/rails runner -e production '\''Server.ping'\'''

## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.

1st) 有趣的是,尽管我在代码中说明了环境,但似乎并没有改变为开发环境。 2nd) crontab 文件没有更新(我什至不知道是否有一个开始?)

所以我尝试通过运行whenever -w ping来创建一个crontab,但结果是

PS C:\SIS> whenever -w ping
[fail] Couldn't write crontab; try running `whenever' with no options to ensure your schedule file is valid.

所以这是我的问题,任何帮助将不胜感激。

编辑:好的,我决定使用 rufus-scheduler 来经常更新我的服务器列表。我测试了每分钟在表中创建一个新服务器是否可行,这似乎可行。 但是,因为我无法阻止它创建服务器,所以我将其从捆绑包中删除,删除了“task_scheduler”文件,然后重新安装它并像以前一样完成所有操作。 但是,当我现在尝试运行服务器时,出现以下错误:

C:/SIS/config/initializers/task_scheduler.rb:1:in '<top (required)>': undefined method 'start_new' for Rufus::Scheduler:
Module (NoMethodError)
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in 'load'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in 'block in load'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in 'block in load_dependency'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in 'new_constants_in'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in 'load_dependency'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in 'load'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/engine.rb:201:in 'block (2 levels) in <class:Engine>'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/engine.rb:200:in 'each'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/engine.rb:200:in 'block in <class:Engine>'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:25:in 'instance_exec'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:25:in 'run'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:50:in 'block in run_initializers'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:49:in 'each'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:49:in 'run_initializers'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:134:in 'initialize!'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:77:in 'method_missing'
        from C:/SIS/config/environment.rb:5:in '<top (required)>'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in 'require'

        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in 'block in require'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in 'block in load_dependency'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in 'new_constants_in'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in 'load_dependency'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in 'require'

        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:103:in 'require_environment!'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:22:in '<top (required)>'
        from script/rails:6:in 'require'
        from script/rails:6:in '<main>'

task_scheduler中的代码如下:

scheduler = Rufus::Scheduler.start_new

scheduler.every '1m' do
#     Server.all.each do
#      |server| server.name.serverUp?
#     end

     Server.ping

end

不确定要使用这两种方法中的哪一种,因此我将第一种方法注释掉了。 帮助将不胜感激。 提前致谢

编辑 好的,终于解决了,不得不把require 'rubygems' require 'rufus/scheduler'放在我的代码顶部。 现在它工作正常:-)

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-3 whenever rufus-scheduler


【解决方案1】:

是的,我同意 Jorg M 的观点,cron 作业是 unix 程序,并且 rufus 调度程序在内部调用 cron unix 进程,因此请远离两者,您应该使用 delayed job ActiveRecord,它可以帮助您做同样的事情。

根据我的经验,还有其他方法,但这是我用过的最好的方法。

祝你好运

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我就是这样解决的:

    我在 AutoIT 中创建了一个小 EXE,该 exe 将从 ini 文件 (config.ini) 中读取要执行的文件/url 的名称

    我命名了 EXE CRON.exe 并让它通过计划任务启动。

    config.ini 中的文件/url 可以是其他可执行文件或 url,脚本会计算出如何运行每种类型。

    AutoIT 是一种非常简单的脚本语言,您可以在下面看到我的 CRON.exe 代码:

    $iniFile='config.ini'
    
    $i=0
    While 1 
        $i+=1
        $file=IniRead($iniFile, 'crons', $i, 'NULL')
        If $file='NULL' Then ExitLoop
    
        If StringInStr($file, 'http://') Then
            InetGet($file, @TempDir, 1)
        Else
            $wd=StringSplit($file, '\', 3)
            $workingDir=''
            For $i=0 To UBound($wd)-2
                $workingDir&=$wd[$i]&'\'
            Next
            RunWait($file, $workingDir)
        EndIf
    WEnd
    

    ini文件示例:

    [CRONS]
    ;paths of the files to execute
    1=C:\anotherEXE.exe
    2=http://mySite.com/myCron.php
    

    所有文件都按顺序执行,没有重叠,如果您想同时执行所有文件,您可以选择将 AutoIT 代码更改为使用 Run 而不是 RunWait

    【讨论】:

    • 这似乎很复杂 :p 我通过使用 rufus-scheduler gem 克服了这个问题,为我完成了这项工作:)。感谢您的回答,尽管 d(•.•)b
    【解决方案3】:

    虽然它远不如 cron 灵活,但 windows 有 AT 命令可能能够解决您的问题,但是您只能安排它每天最多运行一次。要查看如何使用 AT 在命令行上运行 AT /?

    要对正在运行的任务进行更细粒度的控制,您可以使用基于 GUI 的 Windows 任务调度程序。您可以在控制面板 -> 系统和安全 -> 管理工具 -> 任务计划程序中找到它

    【讨论】:

      【解决方案4】:

      Cron 是一个 Unix 程序。它在 Windows 上不存在。

      【讨论】:

      • 那么我应该怎么做才能在 windows 上设置 cron 作业?
      • @teenOmar:Windows 上没有 没有 cron。期间。
      猜你喜欢
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 2012-01-23
      • 2017-11-05
      • 2015-03-05
      • 2012-03-26
      相关资源
      最近更新 更多