From: Glen Holcomb Date: 2008-12-05T23:24:43+09:00 Subject: Re: Force a program to stop if runtime exceeds given duration ------=_Part_28551_12673944.1228487366565 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Fri, Dec 5, 2008 at 7:25 AM, Glen Holcomb wrote: > On Fri, Dec 5, 2008 at 7:08 AM, Glen Holcomb wrote: > >> On Fri, Dec 5, 2008 at 6:59 AM, Aldric Giacomoni <"aldric[remove]"@ >> trevoke.net> wrote: >> >> > Everybody automatically assumes that rubyists are using Linux - sadly, >> in >> > the medical field Linux is not an option unless you're working with the >> > modality itself. Medical software is almost all Windows-based.. So, >> Windows >> > is where I am for this issue and Windows is where I remain :( >> > If it were for an outside-Ruby solution, I can think of tons of options. >> I >> > just want a program which works and does everything all by itself.. >> Because >> > if I have to deploy this to 400 machines, I don't wanna have to do any >> more >> > setup work than absolutely required. >> > >> > >> > Ron Fox wrote: >> > >> >> See http://www.ruby-doc.org/core-1.9/classes/Process.html#M003012 >> >> >> >> It and sleep have all the tools you need to do this. The idea is that >> you >> >> have a master process that forks the child process which runs the >> script you >> >> want to time. The master process then loops. The loop >> >> sleeps a bit then calls waitpid with the WNOHANG flag to see if the >> child >> >> has exited. Count the sleep time, or use Time to figure out how long >> you've >> >> been waiting.. Once you've waited long enough, use kill >> >> to kill the child process. If the child has exited.. the master >> process >> >> exits. >> >> >> >> The length of your sleep in the loop of the master process determines >> >> - The precision with which you measure the timeout. >> >> - The latency with which the master process responds to normal child >> exit. >> >> >> >> Ron. >> >> >> >> Aldric Giacomoni wrote: >> >> >> >>> Any idea how to do that? >> >>> Say, I don't want a program to run longer than 5 hours.. How would >> that >> >>> be implemented? >> >>> >> >>> --Aldric >> >>> >> >> >> >> >> >> >> > >> I'm in the same boat here at work. Fork won't work in Windows so that >> option is out. However if the thread or timeout suggestions don't work >> for >> you it might be possible to adjust Robert's suggestion to run as a Windows >> Task. I don't know if you can put a time limit on a task though (don't >> use >> them very often). >> >> -- >> "Hey brother Christian with your high and mighty errand, Your actions >> speak >> so loud, I can't hear a word you're saying." >> >> -Greg Graffin (Bad Religion) >> > > Another option (to expand on my second suggestion) would be to compile the > potentially long running script into an exe using rubyscript2exe and then > launch it from another script system("start script.exe") then after five > hours wake up and check the windows process list for script.exe and if it is > running kill it: > > wmi = > WIN32OLE.connect("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\\\#{name}\\root\\cimv2") > proc_list = wmi.ExecQuery("select * from win32_process") > proc_list.each do |process| > if process.Name == "script.exe" > process.Terminate(8) > end > end > > http://msdn.microsoft.com/en-us/library/aa394372(VS.85).aspx > > You would of course need to require 'win32ole' for this to work. > > > -- > "Hey brother Christian with your high and mighty errand, Your actions speak > so loud, I can't hear a word you're saying." > > -Greg Graffin (Bad Religion) > Sorry copied that from another program of mine without looking at it too closely. You don't need to impersonate on the WIN32OLE.connect and #{name} should be the machine hostname so something like this should work: require 'socket' require 'win32ole' wmi = WIN32OLE.connect("winmgmts:\\\\#{Socket.hostname}\\root\\cimv2") proc_list = wmi.ExecQuery("select * from win32_process") proc_list.each do |process| if process.Name == "script.exe" process.Terminate(8) end end -- "Hey brother Christian with your high and mighty errand, Your actions speak so loud, I can't hear a word you're saying." -Greg Graffin (Bad Religion) ------=_Part_28551_12673944.1228487366565--