               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)
 

        (or srv (recur (inc port)))
        ))))

(defn start-system
  [app-ref app-config {:keys [cache-root] :as config}]
  (when @app-ref

        port-files-ref
        (atom nil)

        shutdown-hook
        (Thread. stop!)

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