001 (ns dog-and-duck.utils.process
002 (:require [clojure.string :refer [split]]))
003
004 (def pid
005 "OK, this is hacky as fuck, but I hope it works. The problem is that the
006 way to get the process id has changed several times during the history
007 of Java development, and the code for one version of Java won't even compile
008 in a different version."
009 (let [java-version (read-string (apply str (take 2
010 (split
011 (System/getProperty "java.version")
012 #"[_\.]"))))
013 cmd (case java-version
014 18 "(let [[_ pid hostname]
015 (re-find
016 #\"^(\\d+)@(.*)\"
017 (.getName
018 (java.lang.management.ManagementFactory/getRuntimeMXBean)))]
019 pid)"
020 (19 110) "(.pid (java.lang.ProcessHandle/current))"
021 111 "(.getPid (java.lang.management.ManagementFactory/getRuntimeMXBean))"
022 ":default")]
023 (eval (read-string cmd))))