sigildocs

(sigil process)

(sigil process) - Process Management Library

Process management operations including spawning subprocesses, environment variable access, and command execution. Native functions are implemented in C with higher-level utilities defined here.

Running Commands

(import (sigil process))

;; Run and wait for completion
(process-run "ls" "-l")

;; Capture output as string
(process-output->string "date")  ; => "Thu Jan 15 14:30:00 2025"

;; Capture output as lines
(process-lines "ls")  ; => ("file1" "file2" ...)

Spawning Processes

(let ((p (process-spawn "cat")))
  (display "Hello\n" (process-stdin p))
  (close-output-port (process-stdin p))
  (display (read-line (process-stdout p)))
  (process-wait p))

Environment

(getenv "HOME")      ; => "/home/user"
(setenv! "MY_VAR" "value")

Exports

process-runprocedure

Run a command and wait for it to complete.

Returns the exit status (0 for success).

(process-run "ls" "-l")  ; => 0
process-spawnprocedure

Spawn a subprocess without waiting.

Returns a process object. Use process-wait to wait for completion and retrieve the exit status.

(let ((p (process-spawn "sleep" "10")))
  (process-kill! p))
process-waitprocedure

Wait for a process to complete.

Returns the exit status.

(process-wait p)  ; => 0
process-kill!procedure

Kill a running process.

(process-kill! p)
process?procedure

Check if a value is a process object.

Check if a process is still running.

(process-alive? p)  ; => #t or #f
process-stdinprocedure

Get the stdin port of a spawned process.

Returns an output port for writing to the process.

Get the stdout port of a spawned process.

Returns an input port for reading the process output.

Get the stderr port of a spawned process.

Returns an input port for reading error output.

getenvprocedure

Get an environment variable.

Returns the value as a string, or #f if not set.

(getenv "HOME")    ; => "/home/user"
(getenv "MISSING") ; => #f
setenv!procedure

Set an environment variable.

(setenv! "MY_VAR" "my-value")
command-lineprocedure

Get the command-line arguments.

Returns a list where the first element is the program name.

(command-line)  ; => ("./my-program" "arg1" "arg2")
exitprocedure

Exit the process with a status code.

(exit 0)  ; success
(exit 1)  ; failure
process-idprocedure

Get the current process ID.

(process-id)  ; => 12345

Check if a command exists in PATH.

(command-exists? "ls")   ; => #t
(command-exists? "foo")  ; => #f

Call a procedure with a spawned process.

The process is automatically waited for when proc returns.

(call-with-process "ls" '("-l")
  (lambda (p)
    (read-line (process-stdout p))))

Run a command and capture its stdout as a string.

Returns #f if the process cannot be started.

(process-output->string "echo" "hello")  ; => "hello"
(process-output->string "date")          ; => "Thu Jan 15..."

Read all content from a port as a string. Helper for process-output->string.

process-linesprocedure

Run a command and return its stdout as a list of lines.

Returns #f if the process cannot be started.

(process-lines "ls")           ; => ("file1" "file2" ...)
(process-lines "ls" "-la")     ; => ("total 42" "drwxr-x..." ...)

Read all lines from a port. Helper for process-lines.