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
Trying to Get Svn2Git Working on Windows
How to Assert That No Route Matches in a Rails Integration Test
Get Available Diskspace in Ruby
How to Create a Folder (If Not Present) with Logger.New
How to Download a File Over Http Using Ruby
Ruby Cannot Find SQLite3 Driver on Windows
"The Ruby Way" (Mixins and Class Reopening) VS. Dependency Injection
Re-Opened Nested Module Anomaly in Ruby
Ruby/Rails - How to Create a Class and Access It from the Controller
How to Combine a Series of PDFs into One Using Ruby
Sorting an Array of Arrays in Ruby
Error When Installing Ruby 2.1.3 with Rvm
Fresh Install of Rvm in Ubuntu Isn't Letting Me Install Gems (Zlib Error)
How to Do Fuzzy Substring Matching in Ruby
How to Make a Method Available to Both My Controller and Model in Rails
How to Use Factorygirl to Create an Attribute Called "Alias"
After Ruby 2.4 Upgrade - Error While Trying to Load The Gem 'Uglifier' (Bundler::Gemrequireerror)