os/process
Running other programs, capturing their output, waiting for them and stopping them.
import os.process
The os.process package runs another program directly, with no shell in between. The program's path and its arguments are given separately, so nothing has to be quoted and spaces or special characters in an argument cause no trouble.
If you need the program's output, run it with StartCaptured, wait for it to finish and then read the output. Call Cleanup when you are done.
Types
Process
struct Process| Name | Type | Description |
|---|---|---|
pid | int = 0 | The process id. |
handle | void* | The process handle on Windows. |
running | bool = false | True while it is still running. |
exit_code | int = 0 | The exit code, once it has finished. |
stdout_path | str = "" | The temporary file holding the captured output. |
stderr_path | str = "" | The temporary file holding the captured error output. |
import os.process
func main:
mut args := Vector {} as Vector<str>
defer args.free()
args.push("hello from the child process")
mut p := process.StartCaptured("/bin/echo", args)
println process.Pid(p) > 0
code := process.Wait(p)
println code, process.ExitCode(p), process.IsRunning(p)
print process.ReadStdout(p)
println "[" + process.ReadStderr(p) + "]"
process.Cleanup(p)
endFunctions
Start
func Start(path: str, args: Vector<str>): ProcessThe example for this item is shown with the ones above: os-process-Start.salam
StartCaptured
func StartCaptured(path: str, args: Vector<str>): ProcessStart, but captures the program's output and error output for you to read later.The example for this item is shown with the ones above: os-process-Start.salam
WaitPid
func WaitPid(pid: int): intimport os.process
func main:
mut args := Vector {} as Vector<str>
defer args.free()
args.push("30")
mut p := process.Start("/bin/sleep", args)
println process.IsRunning(p)
process.Terminate(p)
process.Wait(p)
println process.IsRunning(p)
mut p2 := process.Start("/bin/sleep", args)
process.Kill(p2)
process.WaitPid(process.Pid(p2))
println "both have stopped"
endWait
func Wait(p &: Process): intThe example for this item is shown with the ones above: os-process-Start.salam
Fork
func Fork(): intimport os.process
func main:
pid := process.Fork()
if pid == 0:
println "I am the child"
process.ExitNow(0)
end
process.WaitPid(pid)
println "the child has finished"
endExitNow
func ExitNow(code: int)Fork.The example for this item is shown with the ones above: os-process-Fork.salam
Kill
func Kill(p &: Process): intThe example for this item is shown with the ones above: os-process-Kill.salam
Terminate
func Terminate(p &: Process): intKill.The example for this item is shown with the ones above: os-process-Kill.salam
ReadStdout
func ReadStdout(p: Process): strThe example for this item is shown with the ones above: os-process-Start.salam
ReadStderr
func ReadStderr(p: Process): strThe example for this item is shown with the ones above: os-process-Start.salam
Cleanup
func Cleanup(p &: Process)The example for this item is shown with the ones above: os-process-Start.salam
ExitCode
func ExitCode(p: Process): intThe example for this item is shown with the ones above: os-process-Start.salam
Pid
func Pid(p: Process): intThe example for this item is shown with the ones above: os-process-Start.salam
IsRunning
func IsRunning(p: Process): boolThe example for this item is shown with the ones above: os-process-Start.salam