1

cronjobs和那些有點東西的總新手,以前從來沒有做過,所以現在我試圖在RailsCasts上看到它後,使用插件的方式讓我的手變髒。 所以我試圖爲我的Ruby應用程序運行cron作業,但它似乎不起作用,可能是因爲代碼或可能是因爲我使用Windows 7?在Windows中運行cron時總是運行

下面是代碼從我的服務器模型

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文件中的代碼中有

#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 

我想這兩種方法進行cron作業, (我最近使用的後者,我在服務器模型中創建了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. 

1日)有趣的是,環境似乎並沒有發生變化,以即使我說,它的代碼開發。 二)未更新crontab文件(我甚至不知道是否有存在的一個開始?)

所以我試圖創建一個crontab每當運行whenever -w ping文件,但結果是

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調度程序爲了經常更新我的服務器列表。我測試了它是否可以在每一分鐘創建一個新的服務器到桌面上,而且這似乎工作。 但是,因爲我無法阻止它創建服務器,所以我從包中刪除它,刪除了'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'在我的代碼的頂部。 現在工作正常:-)

+0

對於使用的cygwin在Windows中運行的cron,[這可能是對你有用的(http://stackoverflow.com/questions/707184/how-do-you-run-a-crontab -in-cygwin的上窗口)。 – Zabba

回答

1

儘管它遠沒有cron那麼靈活,但windows有AT命令可能能夠解決您的問題,但是您只能安排它每天運行一次。要查看如何在命令行上使用AT運行AT /?

對於運行任務的更細粒度的控制,您可以使用基於GUI的Windows任務調度程序。你可以控制面板中找到這個 - >系統和安全 - >管理工具 - >任務計劃程序

3

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

+0

那麼我應該怎麼做才能在windows上使用cron作業? – omarArroum

+1

@teenOmar:在Windows上*沒有cron *。期。 –

0

我有同樣的問題,這是我如何解決它:

我在AutoIt中創建了一個小的EXE ,exe會讀取文件/網址的名稱以執行從ini文件(config.ini)

我命名了EXE CRON.exe,並讓它通過調度任務啓動。

config.ini中的文件/ url可以是其他可執行文件或url,腳本將解決如何運行每種類型。

AutoIt的是一個非常簡單的腳本語言,你可以看到下面我CRON.exe代碼:

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

所有文件,以便沒有重疊執行:ini文件的

$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 

例如果要同時執行所有文件,可以選擇將AutoIT代碼更改爲使用Run而不是RunWait

+0

這似乎很複雜:p 我用rufus-scheduler gem克服了這個問題,爲我做了這個工作:)。 感謝您的回答,儘管d(•。))b – omarArroum

0

是的,我同意Jorg M,cron工作是unix程序,也是rufus scheduler在內部調用cron unix進程,因此請遠離兩者,您應該使用delayed job ActiveRecord,它們可以幫助您做到這一點。

這是根據我的經驗,還有其他的方式,但這是我用過的最好的。

祝您好運