Skip to content

Instantly share code, notes, and snippets.

@nathanmarz
Created March 2, 2011 23:38
Show Gist options
  • Save nathanmarz/852010 to your computer and use it in GitHub Desktop.
Save nathanmarz/852010 to your computer and use it in GitHub Desktop.
(defn process-pid
"Gets the pid of this JVM. Hacky because Java doesn't provide a real way to do this."
[]
(let [name (.getName (ManagementFactory/getRuntimeMXBean))
split (.split name "@")]
(when-not (= 2 (count split))
(throw (RuntimeException. (str "Got unexpected process name: " name))))
(first split)
))
(defn exec-command! [command]
(let [[comm-str & args] (seq (.split command " "))
command (CommandLine. comm-str)]
(doseq [a args]
(.addArgument command a))
(.execute (DefaultExecutor.) command)
))
(defn ensure-process-killed! [pid]
;; TODO: should probably do a ps ax of some sort to make sure it was killed
(try
(exec-command! (str "kill -9 " pid))
(catch ExecuteException e
(log-message "Error when trying to kill " pid ". Process is probably already dead."))
))
(defn launch-process [command]
(let [command (seq (.split command " "))
builder (ProcessBuilder. (cons "nohup" command))]
(.start builder)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment