philipbohun.com

Glojure by Example: Spawning Processes

os/exec.Command builds a command. Extra string arguments are the argv. CombinedOutput runs it and returns the combined stdout and stderr plus an error.

(let [cmd (os:exec.Command "printf" "hello from %s" "a process")
      [out err] (.CombinedOutput cmd)]
  (println (nil? err))
  (println (fmt.Sprintf "%s" out)))

A missing program is an error value, not an exception. Replacing the current process with syscall.Exec is not available: the syscall package is not in the default map.

(let [cmd (os:exec.Command "no-such-glj-binary")
      [_ err] (.CombinedOutput cmd)]
  (println (boolean err)))

printf writes the formatted text. The missing program sets the error.

$ glj spawning-processes.glj
true
hello from a process
true

Next example: Signals.