Skip to content

Instantly share code, notes, and snippets.

@DougEverly
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save DougEverly/ce0031d35c18892e50fd to your computer and use it in GitHub Desktop.

Select an option

Save DougEverly/ce0031d35c18892e50fd to your computer and use it in GitHub Desktop.
Restart children if they die
#!/usr/bin/env ruby
@jobs = [
"sleep 1",
"sleep 2",
"sleep 3",
"sleep 4"
]
@running_jobs = Hash.new
trap("CLD") do
begin
cpid = nil
cpid = Process.wait
job = @running_jobs[cpid]
puts "Child died #{cpid}: #{job}"
@running_jobs.delete cpid
fork_job job
rescue Errno::ECHILD
puts "Got Errno::ECHILD"
rescue Exception => e
puts e.message
end
end
def start
@jobs.each do |job|
fork_job job
end
end
def fork_job(job = nil)
if pid = Process.fork # parent
@running_jobs[pid] = job
else # child
puts "starting #{job}"
Process.exec job
end
status
end
def status
@running_jobs.each_pair do |pid, job|
puts "Running #{pid}: #{job}"
end
end
start
sleep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment