Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8456963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:41:05+00:00 2026-06-10T12:41:05+00:00

Say I have a function like below, how do I capture the output of

  • 0

Say I have a function like below, how do I capture the output of the Process.spawn call? I should also be able to kill the process if it takes longer than a specified timeout.

Note that the function must also be cross-platform (Windows/Linux).

def execute_with_timeout!(command)
  begin
    pid = Process.spawn(command)     # How do I capture output of this process?
    status = Timeout::timeout(5) {
      Process.wait(pid)
    }
  rescue Timeout::Error
    Process.kill('KILL', pid)
  end
end

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:41:06+00:00Added an answer on June 10, 2026 at 12:41 pm

    You can use IO.pipe and tell Process.spawn to use the redirected output without the need of external gem.

    Of course, only starting with Ruby 1.9.2 (and I personally recommend 1.9.3)

    The following is a simple implementation used by Spinach BDD internally to capture both out and err outputs:

    # stdout, stderr pipes
    rout, wout = IO.pipe
    rerr, werr = IO.pipe
    
    pid = Process.spawn(command, :out => wout, :err => werr)
    _, status = Process.wait2(pid)
    
    # close write ends so we could read them
    wout.close
    werr.close
    
    @stdout = rout.readlines.join("\n")
    @stderr = rerr.readlines.join("\n")
    
    # dispose the read ends of the pipes
    rout.close
    rerr.close
    
    @last_exit_status = status.exitstatus
    

    The original source is in features/support/filesystem.rb

    Is highly recommended you read Ruby’s own Process.spawn documentation.

    Hope this helps.

    PS: I left the timeout implementation as homework for you 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment
Okay say I have a function like below: -(NSNumber *)calculate{ NSNumber *myNum = [[[NSNumber
let's say I have a simple javascript like so: $(document).ready(function(){ if(login == 1) {
Let's say I have some simple Javascript like: <script> var hello = function(){ alert(Hello
Say I have a function foo that I want to call n times. In
Say I have a function that takes an arbitrary number of arguments (the last
I have a setInterval function like below on a Divbox so if I leave
Let's say we have a thread-safe compare-and-swap function like long CAS(long * Dest ,long
I have created a function that takes a SQL command and produces output that
Let's say you have the below code: function A() { function modify() { x

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.