Last active
August 29, 2015 14:16
-
-
Save DougEverly/ce0031d35c18892e50fd to your computer and use it in GitHub Desktop.
Restart children if they die
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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