Change the ruby process name in top
Dave Thomas had an interesting post on doing this in rails. There's nothing rails specific about the actual process name change code. He uses the $0='name'
approach. When I followed his steps the name was changed in ps
and top
.
In the post he suggests using the c
keyboard command if your version of top doesn't show the short version of the command by default.
Windows Ruby get PID from process name
The sys-proctable gem can do that, here is a minimal example for getting the PID of the ruby.exe process on the system:
require 'sys/proctable'
def get_process_id(name)
Sys::ProcTable.ps.detect{|p| p.name == name}&.pid
end
puts get_process_id("ruby.exe")
That doesn't guarantee you will find the parent process tho, you will instead get the process with the lowest PID.
To actually find the "root process", you need to further select processes by checking if their parent process is either non-existent or a different process:
require 'sys/proctable'
def get_parent_process_id(name)
# generate a hash mapping pid -> process info
processes = Sys::ProcTable.ps.map{|p| [p.pid, p]}.to_h
# find the first matching process that has either no parent or a parent
# that doesn't match the process name we're looking for
processes.values.each do |p|
next if p.name != name
if processes[p.ppid].nil? || processes[p.ppid].name != name
return p.pid
end
end
nil
end
puts get_parent_process_id("chrome.exe")
ruby creating processes and reading output
Use IO.popen
to fork the process (it will return a IO
object with a pid
method corresponding to the forked process). You can select
on the IO object to avoid blocking.
Rails server says port already used, how to kill that process?
Assuming you're looking to kill whatever is on port 3000 (which is what webrick normally uses), type this in your terminal to find out the PID of the process:
$ lsof -wni tcp:3000
Then, use the number in the PID column to kill the process:
$ kill -9 PID
Obtain the name of a unknown process, by the window handle?
I don't know Ruby, but since you are invoking the WinAPI directly anyway, I can give you an example in C. Assuming you already have a HWND
called hWnd
scoped (error handling omitted for brevity):
HANDLE hProcess;
DWORD dwPID;
WCHAR lpFilename[MAX_PATH];
GetWindowThreadProcessId(hWnd, &dwPID)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwPID);
GetModuleFileNameEx(hProcess, NULL, lpFilename, _countof(lpFilename));
CloseHandle(hProcess);
Getting the parent id of a given process in Ruby
You can just remember it in a variable:
parent_pid = Process.pid
Process.fork do
child_pid = Process.pid
puts parent_pid, child_pid
# do stuff
exit
end
Process.wait
# 94791
# 94798
alternatively, if you need the information on the level of the parent process:
parent_pid = Process.pid
child_pid = Process.fork do
# do stuff
exit
end
Process.wait
puts parent_pid, child_pid
# 6361
# 6362
Related Topics
Rails Console Fails with 'Switch to Inspect Mode' in Windows
Combine Thumbnails to One Large Image with Rmagick
Simulating Race Conditions in Rspec Unit Tests
Ruby Rake Load Tasks from a Gem
How to Use Your Own MySQL Database Server with Heroku
How to Split a Directory String in Ruby
How to Completely Wipe Rubygems Along with Rails etc
How Do Open a File for Writing Only If It Doesn't Already Exist in Ruby
How to Restart Rails from Within Rails
Ways to Define a Global Method in Ruby
Bundle Install Tries to Use Cache File
How to Create a Folder (If Not Present) with Logger.New