Nick Heiner
Nick Heiner

Reputation: 122450

Having a IO.popen command be killed when the caller process is killed

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?

Upvotes: 2

Views: 2945

Answers (2)

Reck
Reck

Reputation: 8782

Note that the above approach does not work when the signal is a SIGKILL. In order to fix this, you could implement a pipe-based approach as shown in: https://github.com/vaneyckt/Adeona/blob/master/lib/adeona.rb

Upvotes: 2

Casper
Casper

Reputation: 34308

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

Upvotes: 5

Related Questions