質問

Total newbie at cronjobs and that kinda stuff, never done it before, so now I tried to get my hands dirty using whenever as a plugin, after seeing it on RailsCasts. So I am trying to run a cron job for my Ruby application, but it seems like it's not working, maybe becuase of the code or maybe because of me using Windows 7?

Here is the code from my Server model

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

And here is the code from my schedule.rb file which has the cron job in it

#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

I tried both methods out, (the latter I used most recently, to which I created the self.ping method in the Server model.

So when I run whenever in my rails directory, this is the output I get:

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) Funnily enough, the environment doesn't seem to have changed to development even though I stated it in the code. 2nd) The crontab file wasn't updated (I don't even know if there existed one to begin with?)

So I tried to create a crontab whenever file by running whenever -w ping, but the results was

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

So this is my problem, any help would be appreciated.

Edit: Ok, I decided to use rufus-scheduler in order to update my Server list frequently. I tested out whether it would work to create a new server into the table every mintues, and that seemed to work. However, because I wasn't able to stop it from creating servers, I removed it from the bundle, deleted the 'task_scheduler' file and then re-installed it and done everything like before. However, when I now try to run the server, I get the following error:

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>'

The code in the task_scheduler is as follows:

scheduler = Rufus::Scheduler.start_new

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

     Server.ping

end

Not sure which of the two methods to use, hence I commented the first one out. Help would be greatly appreciated. Thanks in advance

Edit Ok, finally solved it, had to put require 'rubygems' require 'rufus/scheduler' on the top of my code. Now it works fine :-)

役に立ちましたか?

解決

Although it's nowhere near as flexible as cron, windows has the AT command which may be able to solve your problems, however you can only schedule it to run at most once a day. To see how to use AT run AT /? on your command line.

For much more fine-grained control over running tasks you can use the GUI based windows task scheduler. you can find this in Control Panel -> System and Security -> Administrative Tools -> Task Scheduler

他のヒント

Cron is a Unix program. It doesn't exist on Windows.

I had the same problem, this is how I solved it:

I created a small EXE in AutoIT, the exe will read the names of files/urls to execute from an ini file (config.ini)

I named the EXE CRON.exe and had it start via schedulled tasks.

The files/urls in the config.ini can be either other executables or urls, the script will work out how to run each type.

AutoIT is a very simple scripting language, you can see my CRON.exe code below:

$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

Example of ini file:

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

All files are executed in order with no overlap, you can optionally change the AutoIT code to use Run instead of RunWait if you want to execute all files at the same time.

Yes I agree with Jorg M, cron job is unix program and also rufus scheduler internally call cron unix process so stay away from both and you should use delayed job ActiveRecord which help you to do the same.

This is as per my experience, there could other way but this is best which I ever used.

Best Luck

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top