Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > exception for program running on ruby code

Reply
Thread Tools

exception for program running on ruby code

 
 
Ahmad Azizan
Guest
Posts: n/a
 
      11-11-2009
Hello,

I'm trying to get an output from /var/log/syslog for certain amount of
time by executing %x[sudo tail -f /var/log/syslog]

Is it possible to set a timer for the execution, and if the time end,
the execution will end (like we do Ctrl+C) ?

Thank you
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Ahmad Azizan
Guest
Posts: n/a
 
      11-11-2009
Ahmad Azizan wrote:
> Hello,
>
> I'm trying to get an output from /var/log/syslog for certain amount of
> time by executing %x[sudo tail -f /var/log/syslog]
>
> Is it possible to set a timer for the execution, and if the time end,
> the execution will end (like we do Ctrl+C) ?
>
> Thank you


I think I've got the solution,

#!/usr/bin/ruby

require 'timeout'
begin
status = Timeout::timeout(10) do
%x[sudo tail -f /var/log/messages]
end
rescue Timeout::Error
puts 'execution expired'
end

This might do the job

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      11-11-2009
Ahmad Azizan wrote:
> Ahmad Azizan wrote:
>> Hello,
>>
>> I'm trying to get an output from /var/log/syslog for certain amount of
>> time by executing %x[sudo tail -f /var/log/syslog]
>>
>> Is it possible to set a timer for the execution, and if the time end,
>> the execution will end (like we do Ctrl+C) ?
>>
>> Thank you

>
> I think I've got the solution,
>
> #!/usr/bin/ruby
>
> require 'timeout'
> begin
> status = Timeout::timeout(10) do
> %x[sudo tail -f /var/log/messages]
> end
> rescue Timeout::Error
> puts 'execution expired'
> end
>
> This might do the job
>


I don't think you will get a value in status that way. Try this:

require 'timeout'

output = []
begin
Timeout.timeout 5 do
IO.popen("ping 192.168.1.1") do |pipe|
while line = pipe.gets
output << line
end
end
end
rescue TimeoutError
end

puts output


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      11-11-2009
Ahmad Azizan wrote:
> Ahmad Azizan wrote:
>> Hello,
>>
>> I'm trying to get an output from /var/log/syslog for certain amount of
>> time by executing %x[sudo tail -f /var/log/syslog]
>>
>> Is it possible to set a timer for the execution, and if the time end,
>> the execution will end (like we do Ctrl+C) ?
>>
>> Thank you

>
> I think I've got the solution,
>
> #!/usr/bin/ruby
>
> require 'timeout'
> begin
> status = Timeout::timeout(10) do
> %x[sudo tail -f /var/log/messages]
> end
> rescue Timeout::Error
> puts 'execution expired'
> end
>
> This might do the job
>


I don't think you will get a value in status that way. Try this:

require 'timeout'

output = []
begin
Timeout.timeout 5 do
IO.popen("ping 192.168.1.1") do |pipe|
while line = pipe.gets
output << line
end
end
end
rescue TimeoutError
end

puts output


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
Ahmad Azizan
Guest
Posts: n/a
 
      11-11-2009
Joel VanderWerf wrote:

> I don't think you will get a value in status that way. Try this:
>
> require 'timeout'
>
> output = []
> begin
> Timeout.timeout 5 do
> IO.popen("ping 192.168.1.1") do |pipe|
> while line = pipe.gets
> output << line
> end
> end
> end
> rescue TimeoutError
> end
>
> puts output


Thanks for the correction. Actually I'm running javascript interpreter
program called spidermonkey and it does not require storing values.
After finished it will generate a file. However sometimes if the
javascript code is not properly coded, or with infinite loops, the
javascript interpreter will just hang in there waiting. So I dont want
that to happen. Need to get it stop if it tooks more than 10 seconds.

I've tried using the timeout, and it worked. However, I've found out
that the program's process still running in background, which in this
case it did not terminate after the timeout. How can I stop the process
after reaching the timeout?

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm getting an unhandled win32 exception while running this program Maxx C Programming 14 04-04-2011 10:36 AM
Does Ruby support exception wrapping (exception chaining)? Hartin, Brian Ruby 15 02-23-2011 07:02 AM
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Help !I want to write a program to count the running time of another program freehomesp@yahoo.com.cn C Programming 1 08-12-2005 06:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57