I have a script below, using Thor to run, like rake job management ARGV.
require "thor"
class Run < Thor
desc "start", "start script"
def start
p1 = fork{ loop... }
p2 = fork{ loop... }
Process.detach(p1)
Process.waitpid(p1)
end
desc "stop", "stop script"
def stop
end
end
Run.start
When launched, ruby script.rb startit generates two subprocesses (total three). My question is how to kill all processes when I execute ruby script.rb stop. I saw on the Internet that, first, I have to save the parent pid process to a file, and when it stops, I read it and kill it. The problem is that killing a parent does not kill children. Therefore, I could save all three pids in a file and kill one by one later.
I ask myself how to do this correctly, and whether I work correctly with processes inside start.
source
share