Skip to content

Instantly share code, notes, and snippets.

@simon-brooke
Created December 21, 2022 17:01
Show Gist options
  • Save simon-brooke/2f10b429ac472c2c41ff3c47a3250bf6 to your computer and use it in GitHub Desktop.
Save simon-brooke/2f10b429ac472c2c41ff3c47a3250bf6 to your computer and use it in GitHub Desktop.
Getting the process id from Clojure
(def pid
"OK, this is hacky as fuck, but I hope it works. The problem is that the
way to get the process id has changed several times during the history
of Java development, and the code for one version of Java won't even compile
in a different version."
(let [java-version (read-string (apply str (take 2
(split
(System/getProperty "java.version")
#"[_\.]"))))
cmd (case java-version
18 "(let [[_ pid hostname]
(re-find
#\"^(\\d+)@(.*)\"
(.getName
(java.lang.management.ManagementFactory/getRuntimeMXBean)))]
pid)"
(19 110) "(.pid (java.lang.ProcessHandle/current))"
111 "(.getPid (java.lang.management.ManagementFactory/getRuntimeMXBean))"
":default")]
(eval (read-string cmd))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment