2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-05 08:59:27 +08:00

demo script for demonizing using fork exec

minor refinements to demon
This commit is contained in:
Sam 2018-01-11 13:51:52 +11:00
parent 6dad7dfcec
commit fc05164667
3 changed files with 72 additions and 7 deletions

31
script/demon_test/child Executable file
View file

@ -0,0 +1,31 @@
#!/usr/bin/env ruby
$parent_pid = ARGV[0].to_i
puts "Hello from #{Process.pid} my parent is #{$parent_pid}"
Thread.new do
def alive?(pid)
Process.kill(0, pid)
true
rescue
false
end
while true
begin
unless alive?($parent_pid)
STDERR.puts "Parent was terminated!"
Process.kill "TERM", Process.pid
sleep 10
Process.kill "KILL", Process.pid
end
rescue => e
STDERR.puts "URGENT monitoring thread had an exception #{e}"
end
sleep 1
end
end
sleep

View file

@ -0,0 +1,25 @@
require File.expand_path("../../../config/environment", __FILE__)
puts "Parent is now loaded"
class ForkExecDemon < Demon::Base
def self.prefix
"fork-exec-child"
end
def run
if @pid = fork
write_pid_file
return
end
exec "./child #{parent_pid}"
end
end
ForkExecDemon.start(1, verbose: true)
while true
ForkExecDemon.ensure_running
sleep 0.1
end