# File lib/rbg.rb, line 189
    def master_process
      # Log the master PID
      logger.info "New master process: #{Process.pid}"
      STDOUT.flush
      
      # Set the process name
      $0="#{self.config.name}[Master]"
      
      # Fork a Parent process
      # This will load the before_fork in a clean process then fork the script as required
      self.start_parent
      
      # A restart is not required yet...
      restart_needed = false
      
      # If we get a USR1, set this process as waiting for a restart
      Signal.trap("USR1", proc {
        logger.info "Master got a USR1."
        restart_needed = true
      })
      
      # If we get a TERM, send the existing workers a TERM before bowing out
      Signal.trap("TERM", proc {
        logger.info "Master got a TERM."
        STDOUT.flush
        kill_child_processes
        Process.exit(0)
      })
      
      # INT is useful for when we don't want to background
      Signal.trap("INT", proc {
        logger.info "Master got an INT."
        STDOUT.flush
        kill_child_processes
        Process.exit(0)
      })

      Signal.trap('HUP', proc {})
      
      # Main loop, we mostly idle, but check if the parent we created has died and exit
      loop do
        sleep 2
        if restart_needed
          STDOUT.flush
          self.kill_child_processes
          load_config
          self.start_parent
          restart_needed = false
        end
        
        self.child_processes.each do |id, opts|
          begin
            Process.getpgid(opts[:pid])
          rescue Errno::ESRCH
            logger.info "Parent process #{config.name}[#{id}] has died (from PID #{opts[:pid]}), exiting master"
            Process.exit(0)
          end
        end
      end
    end