How to timeout subprocess in Ruby -


i want test process working run:

cmd = "my unix command" results = `#{cmd}` 

how can add timeout command if takes more x seconds can assume not working?

ruby ships witrh timeout module.

require 'timeout' res = "" status = timeout::timeout(5) {res = `#{cmd}`} rescue timeout::error  # bit of experimenting:  res = nil status = timeout::timeout(1) {res = `sleep 2`} rescue timeout::error  p res    # nil p status # timeout::error  res = nil status = timeout::timeout(3) {res = `sleep 2`} rescue timeout::error  p res    # "" p status # "" 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -