# Exception for program running on ruby code

**URL:** <https://www.ruby-forum.com/t/exception-for-program-running-on-ruby-code/178839>\
**Category:** Ruby\
**Created:** [November 11, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/exception-for-program-running-on-ruby-code/178839 "2009-11-11T23:40:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 11, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/exception-for-program-running-on-ruby-code/178839/1 "2009-11-11T23:40:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 12, 2009, 12:00am UTC](https://www.ruby-forum.com/t/exception-for-program-running-on-ruby-code/178839/2 "2009-11-12T00:00:44Z")

</div>

Ahmad A. 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

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 12, 2009, 12:10am UTC](https://www.ruby-forum.com/t/exception-for-program-running-on-ruby-code/178839/3 "2009-11-12T00:10:50Z")

</div>

Ahmad A. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 12, 2009, 12:11am UTC](https://www.ruby-forum.com/t/exception-for-program-running-on-ruby-code/178839/4 "2009-11-12T00:11:26Z")

</div>

Ahmad A. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 12, 2009, 12:26am UTC](https://www.ruby-forum.com/t/exception-for-program-running-on-ruby-code/178839/5 "2009-11-12T00:26:57Z")

</div>

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?
