# File lib/daemons/application.rb, line 370
    def stop(no_wait = false)
      unless running?
        zap
        return
      end

      pid = @pid.pid

      # Catch errors when trying to kill a process that doesn't
      # exist. This happens when the process quits and hasn't been
      # restarted by the monitor yet. By catching the error, we allow the
      # pid file clean-up to occur.
      begin
        Process.kill(SIGNAL, pid)
      rescue Errno::ESRCH => e
        @report.output_message("#{e} #{pid}")
        @report.output_message('deleting pid-file.')
      end

      unless no_wait
        if @force_kill_waittime > 0
          @report.stopping_process(group.app_name, pid)
          $stdout.flush

          begin
            Timeout.timeout(@force_kill_waittime, TimeoutError) do
              while Pid.running?(pid)
                sleep(0.2)
              end
            end
          rescue TimeoutError
            @report.forcefully_stopping_process(group.app_name, pid)
            $stdout.flush

            begin
              Process.kill('KILL', pid)
            rescue Errno::ESRCH
            end

            begin
              Timeout.timeout(20, TimeoutError) do
                while Pid.running?(pid)
                  sleep(1)
                end
              end
            rescue TimeoutError
              @report.cannot_stop_process(group.app_name, pid)
              $stdout.flush
            end
          end
        end

      end

      sleep(0.1)
      unless Pid.running?(pid)
        # We try to remove the pid-files by ourselves, in case the application
        # didn't clean it up.
        zap!

        @report.stopped_process(group.app_name, pid)
        $stdout.flush
      end
    end