               socket-repl
               (assoc :socket-repl (:port socket-repl)))
             )))
+
+    (let [nrepl-port-file (io/file ".nrepl-port")]
+      (when-not (.exists nrepl-port-file)
+        (spit nrepl-port-file (str (:port nrepl)))
+        (swap! port-files-ref assoc :nrepl-port nrepl-port-file)
+        (.deleteOnExit nrepl-port-file)))
+
+    (. (Runtime/getRuntime) (addShutdownHook shutdown-hook))
 
     (spit pid-file pid)
 

        port-files-ref
        (atom nil)

        app
        (-> {::started (System/currentTimeMillis)
             :server-pid pid
             :config config
             :ssl-context ssl-context
             :http {:port (or https-port http-port)
                    :http-port http-port

       (log/warn t# "shutdown failed" ~(pr-str body))
       (discard-println ~(str "shutdown failed: " (pr-str body))))))

(defn shutdown-system [{:keys [shutdown-hook http port-files-ref socket-repl cli-repl nrepl] :as app}]
  (discard-println "shutting down ...")
  (. (Runtime/getRuntime) (removeShutdownHook shutdown-hook))

  (do-shutdown
    (doseq [port-file (vals @port-files-ref)]
      (.delete port-file)))

  (when socket-repl
    (do-shutdown (socket-repl/stop socket-repl)))

