Killing Process Group from Ruby Kills My Whole Computer

Killing entire process tree in ruby

You can start the process in a new process group by passing pgroup: true: (see the docs for Process.spawn for available options)

stdin, stdout, wait_thr = Open3.popen2e(command, pgroup: true)

The whole process group can then be kill-ed via its process group ID by prepending the signal with a minus sign:

If signal is negative (or starts with a minus sign), kills process groups instead of processes.

pgid = Process.getpgid(wait_thr.pid)
Process.kill '-TERM', pgid

Kill process and sub-processes in Ruby on Windows

I eventually solved this in the following manner

First I installed the sys-proctable gem

gem install 'sys-proctable'

then used the originally posted code to spawn the process, and the following to kill it (error handling omitted for brevity)

require 'win32/process'
require 'sys/proctable'
include Win32
include Sys

to_kill = .. // PID of spawned process
ProcTable.ps do |proc|
to_kill << proc.pid if to_kill.include?(proc.ppid)
end

Process.kill(9, *to_kill)
to_kill.each do |pid|
Process.waitpid(pid) rescue nil
end

You could change the kill 9 to something a little less offensive of course, but this is the gist of the solution.

Cross-platform way to Check if a (non-child) Process is Running

The easiest way is to test which operation system your ruby is running, and then through the command line use a simple test on linux or windows to check if your program is running.

The easiest way often is not the best way. Yet, be prepared to face some strange code, for example, how to simple kill a process on windows. It is your call.

I would love someone say I'm wrong and that there is a great way to manage processes in ruby being cross-platform. Please do.

How to stop/kill server (development) in rubymine

Got similar issue with Rubymine 3.1 (it do not seems to be a Rubymine issue) and rails 3.0.9 (it also do not seems to be a Rails issue). Om my computer I have the same problem by running the erver from the console.
Try running rails s in the console from your application directory, and stop the process (server) by pressing Ctrl+C.

On my Ubuntu 11.4 + Rails 1.9.2 (via RVM) it doesn't work as well, proving the issue do not come from Rubymine.

Looking around the web, it is quite common that ruby server "stale", but it is the very first time I see it, and didn't get the solution for now (sorry for that).

Last but not least ! Process stale with all ruby server I tried : Webrick, Mongrel & Thin. Making me think, it is a ruby issue, it seems that the stop signal does not arrive to the server...

I'll also be glad, if somebody has some more clues to fix this.



Related Topics



Leave a reply



Submit