Force a program to stop if runtime exceeds given duration

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

There are various methods for real time computing. It really depends on how
exact you want to be.

···

On Thu, Dec 4, 2008 at 10:04 AM, Aldric Giacomoni <"aldric[remove]"@ trevoke.net> 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

--
"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)

require 'timeout'

HOURS = 60 * 60 # seconds, minutes
time_limit = 5 * HOURS

begin
  Timeout.timeout(time_limit) do
    # your program goes here
  end
rescue Timeout::Error
    puts "Program has exceeded allotted time, exiting."
end

···

--
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

See module Process - RDoc Documentation

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

--
Ron Fox
NSCL
Michigan State University
East Lansing, MI 48824-1321

Sorry that answer was a bit useless. Here is a very simple example:

You could have two threads, your main thread and a sub-thread. At the
beginning of execution put the sub-thread to sleep for 5 hours (18000
seconds). Then put whatever code you want in the main thread. After 5
hours the sub-thread will wake (assuming there is no blocking I/O or other
impediment). In the sub-thread simply call exit.

This is an insanely simple example though.

···

On Thu, Dec 4, 2008 at 10:32 AM, Glen Holcomb <damnbigman@gmail.com> wrote:

On Thu, Dec 4, 2008 at 10:04 AM, Aldric Giacomoni <"aldric[remove]"@ > trevoke.net> 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

There are various methods for real time computing. It really depends on
how exact you want to be.

--
"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)

--
"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)

I don't need atomic precision - I need to stop a task that runs overnight (being 5 minutes off would not be a problem) so that it's not still running when a user gets to their machine.
I did a search on 'real-time computing' but got nothing relevant to me - maybe I did the search wrong.. ?

Glen Holcomb wrote:

···

[Note: parts of this message were removed to make it a legal post.]

On Thu, Dec 4, 2008 at 10:04 AM, Aldric Giacomoni <"aldric[remove]"@ > trevoke.net> 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

There are various methods for real time computing. It really depends on how
exact you want to be.

In particular, this can't interrupt system calls and can even be foiled by something as "simple" as a nasty, poorly-written, backtracking regexp that will eventually complete, but might consume resources for several more hours. (This one bit me once, can you tell? :wink:

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Dec 4, 2008, at 1:54 PM, Avdi Grimm wrote:

require 'timeout'

HOURS = 60 * 60 # seconds, minutes
time_limit = 5 * HOURS

begin
Timeout.timeout(time_limit) do
   # your program goes here
end
rescue Timeout::Error
   puts "Program has exceeded allotted time, exiting."
end

-- Avdi

Home: http://avdi.org
Developer Blog: Avdi Grimm, Code Cleric
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

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 :frowning:
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 module Process - RDoc Documentation

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

If there is a chance that your process will be blocking for whatever reason
you could always write one script that launches the other process and after
5 hours looks for the process it started and kills it then exits. That
isn't super clean but would work.

···

On Thu, Dec 4, 2008 at 10:39 AM, Aldric Giacomoni <"aldric[remove]"@ trevoke.net> wrote:

I don't need atomic precision - I need to stop a task that runs overnight
(being 5 minutes off would not be a problem) so that it's not still running
when a user gets to their machine.
I did a search on 'real-time computing' but got nothing relevant to me -
maybe I did the search wrong.. ?

Glen Holcomb wrote:

[Note: parts of this message were removed to make it a legal post.]

On Thu, Dec 4, 2008 at 10:04 AM, Aldric Giacomoni <"aldric[remove]"@ >> trevoke.net> 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

There are various methods for real time computing. It really depends on

how
exact you want to be.

--
"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)

Glen Holcomb wrote:

You could have two threads, your main thread and a sub-thread. At the
beginning of execution put the sub-thread to sleep for 5 hours (18000
seconds). Then put whatever code you want in the main thread. After 5
hours the sub-thread will wake (assuming there is no blocking I/O or other
impediment). In the sub-thread simply call exit.

This is an insanely simple example though.

I think .. Your simple example actually fits my needs :slight_smile:

Thanks for the help and the various hints about thinking in threads.

Fwiw, coreutils 7.0 will have a `timeout' program; it runs a command with
bounded time.

···

--
Jos Backus
jos at catnook.com

As others have noted, using timeout is fraught with peril, and I
myself would not use it for anything I wanted to reliably recover and
keep running. For just killing a process, though, it may be what
you're looking for. Although for that matter, you could just do:

  ruby myprocess.rb &
  echo kill $! | at now + 5 hours
  fg

···

--
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

If you start your job via crontab another reasonably simple approach
would be to store the PID in a file on startup and schedule another
script 5 hours later that simply sends a kill -INT (or whatever). The
you can handle the signal in your main script and do whatever you need
to to to clean up and exit.

Kind regards

robert

···

2008/12/4 Aldric Giacomoni <"aldric[remove]"@trevoke.net>:

I don't need atomic precision - I need to stop a task that runs overnight
(being 5 minutes off would not be a problem) so that it's not still running
when a user gets to their machine.

--
remember.guy do |as, often| as.you_can - without end

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).

···

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 :frowning:
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 module Process - RDoc Documentation

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

--
"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)

Hmm to bad wine is not making more progress. However if you cannot
bring WIndows to Linux, there are still some ways to mimick an OS
behaviour on Windows, cygwin might be an overkill for this but what
about Msys?
Sorry for the OT but this still might be helpful for some folks.
http://mingw.org/node/18

Mingw/Msys is minimal but you get bash and actually that is all you need

ruby <your prog> &
echo "kill $!" > /tmp/xxx
sleep 300 && bash /tmp/xxx

potentially "nohup" the last command
N.B. You can even run ruby in the foreground if you get the PID and issue
sleep 300 && kill <PID>
in a different shell

Cheers
Robert

···

On Fri, Dec 5, 2008 at 2:59 PM, 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 :frowning:

Maybe you should look for the timeout tool on the Web. There is a
somehow old association between timeout and Ara in my brain.

HTH
R.

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

You would of course need to require 'win32ole' for this to work.

···

On Fri, Dec 5, 2008 at 7:08 AM, Glen Holcomb <damnbigman@gmail.com> 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 :frowning:
> 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 module Process - RDoc Documentation
>>
>> 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)

--
"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)

I believe Windows has also a scheduling service. So you could use
that instead of cron.

Cheers

robert

···

2008/12/5 Glen Holcomb <damnbigman@gmail.com>:

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).

--
remember.guy do |as, often| as.you_can - without end

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)

It is possible - that is currently how I'm doing it. Part of my MO, however, is to minimize required work and setup -- the fewer steps, the fewer chances of screw-ups !

Hi,

I am a student at MIT founding a non-profit, with the mission to
increase the number of college students who do meaningful community
service. We are a young team, and looking for individuals with more
rails experience to help with the development of the site.

While the mission may sound generic, I ensure you we have very unique
ideas with a lot of growth potential. Also, although this is a 501(c)(3)
venture, our business model is still very profitable. I can provide more
information upon request. Please respond to maarnold [-!at!-] mit
[!-dot-!] edu.

Thanks,

PS- Sorry if this is a repost, not sure if it went through.

···

---
Marvin Arnold
Massachusetts Institute of Technology
Department of Computer Science and Electrical Engineering
Class of MMX