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

  • Home
  • SEARCH
  • 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 9253017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:06:34+00:00 2026-06-18T11:06:34+00:00

I have a Ruby script that starts a subprocess. I want them to be

  • 0

I have a Ruby script that starts a subprocess. I want them to be killed when the overall process is killed.

IO.popen('testacular start unit.conf.js', 'w')

Run my script:

user.name:/my/repo [git: my-branch] $ ruby my-script.rb

Output from testacular:

user.name:/my/repo [git: my-branch] $ info: Testacular server started at http://localhost:8000/
info (launcher): Starting browser PhantomJS
info (PhantomJS 1.7): Connected on socket id uVAO41Q2niyLA8AqbZ8w
PhantomJS 1.7: Executed 44 of 44 SUCCESS (0.213 secs / 0.115 secs)

Hit Control-C to kill the process. Check the running processes:

user.name:/my/repo [git: my-branch] $ ps
  PID TTY           TIME CMD
 # ...
39639 ttys019    0:01.28 node /usr/local/bin/testacular start unit.conf.js
39649 ttys019    0:00.09 node /usr/local/bin/phantomjs /var/folders/2p/dklw3xys2n3f4hqmx73zvz6w0000gp/T/testacular-61981618/capture.js
39650 ttys019    0:00.82 /usr/local/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs /var/folders/2p/dklw3xys2n3f4hqmx73zvz6w0000gp/T/testacular-61981618/capture.js

We can see that the testacular process is still running.

Kill it manually and see the typical testacular shutdown output:

user.name:/my/repo [git: my-branch] $ kill 39639
info: Disconnecting all browsers
error (launcher): Cannot start PhantomJS

user.name:/my/repo [git: my-branch] $ 

Is there a way to make the IO.popen call such that I don’t have to manually kill testacular later?

  • 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-18T11:06:35+00:00Added an answer on June 18, 2026 at 11:06 am

    Yes, you just need to install a signal handler in your main process to trap Ctrl-C (SIGINT) and then send that signal to the child process.

    This example should explain things:

    # Start child and save its pid
    io  = IO.popen("sleep 600")
    pid = io.pid
    
    # Print the output of the ps command (just for demonstration)
    puts "Checking #{pid} ..."
    system("ps #{pid}")
    
    puts "Installing signal handler..."
    
    Signal.trap("INT") {
      # Ctrl-C was pressed...
      puts "Caught interrupt - killing child..."
    
      # Kill child process...
      Process.kill("INT", pid)
    
      # This prevents the process from becoming defunct
      io.close
    
      # Just for demonstration purposes - check that it died
      puts "Checking #{pid} ..."
      system("ps #{pid}")
    
      # Forward the INT signal back to the parent
      # ...or you could just call "exit" here too.
      puts "Forwarding signal to myself..."
      Signal.trap("INT", "DEFAULT")
      Process.kill("INT", 0)
    }
    
    # Make the parent "do some stuff"...
    puts "Sleeping parent..."
    sleep 600
    

    Output:

    > ruby popen_test.rb
    Checking 2474 ...
      PID TTY      STAT   TIME COMMAND
     2474 pts/0    S+     0:00 sleep 600
    Installing signal handler...
    Sleeping parent...
    
    # Press Ctrl-C ...  
    
    Caught interrupt - killing child...
    Checking 2474 ...
      PID TTY      STAT   TIME COMMAND
    Forwarding signal to myself...
    kill.rb:20: Interrupt
            from kill.rb:24:in `call'
            from kill.rb:24:in `sleep'
            from kill.rb:24
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created one ruby script that I want to run with some flags
I have a ruby script that I want to run 24/7 on my vps(
I have Ruby script that creates a proxy so that I can make HTTP
so i have ruby script that simply writes to a test.txt file with hello
I have a ruby script that opens a browser. I have it set to
I have a ruby script that does a few perforce operations (through the scripting
I have a ruby script that is simultaneously and asynchronously receiving and displaying messages
I have this ruby script that allows me to enter the name of a
I have a simple Ruby script that uses the rb-appscript gem to control iTunes,
I have a Ruby script (1.8.7) that sets up its own interactive shell for

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.