os.process
Running other programs, talking to them through pipes, reading with a time limit, telling an exit code from a signal, and stopping them.
import os.process
- Pass arguments exactly as they are
- Exit code, output and error output
- Run several programs at once
- A helper that runs a program and returns its output
- Stop a program
- Talk to a running child
- Read with a time limit
- Exit code or signal
- Run with a timeout
- Stop a child and everything it started
- Types
- Constants and variables
- Functions
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. Use it to call tools such as git, ffmpeg or a compiler with file names that came from a user. For a quick one-line shell command use os (Run, RunCapture) or os.shell; when a program cannot start at all, the reason is recorded in os.error.
A run has up to four steps: Start or StartCaptured launches the program and returns at once, Wait blocks until it exits and returns the exit code, ReadStdout and ReadStderr read what a captured run printed, and Cleanup deletes the two temporary files that held it. A captured program gets an empty standard input.
For a program you talk to while it runs, with pipes, time limits and exit codes or signals, use Command, Launch and Child, described in the sections below. For a program that must see a terminal, use os.pty.
What people get wrong:
- Treating
IsRunningas a live check. It reports a flag thatStartsets and onlyWait,KillorTerminateclear; it stays true after the program has exited on its own. - Killing without waiting.
KillandTerminateonly send the signal. CallWaitafterwards, or the finished process stays in the process table as a zombie. - Expecting a timeout from
Wait.Waiton aProcesswaits as long as the program runs. To give up after a while use aChild(see below), whoseWaittakes a time limit. - Forgetting
Cleanup. Each captured run leaves two files in the temporary folder until you call it.
On Windows the arguments are joined into one command line with quotes around those that contain spaces, and the ANSI CreateProcessA starts the program. An argument that itself contains a ", or text outside the system code page such as Persian, does not arrive intact there; Fork and WaitPid return -1.
Pass arguments exactly as they are
Each element of the argument vector reaches the program as one argument, byte for byte. Spaces, quotes, a $ or a ; mean nothing special, because no shell reads them. That makes this package the safe way to pass file names and other text that came from a user.
import os.process
func main:
mut args := Vector {} as Vector<str>
defer args.Free()
args.Push("[%s]\n")
args.Push("two words")
args.Push("it's quoted")
args.Push("$HOME")
args.Push("*.txt; rm -rf x")
mut p := process.StartCaptured("printf", args)
process.Wait(p)
print process.ReadStdout(p)
process.Cleanup(p)
endThe program name is looked up on the PATH like in a terminal, so printf works without a full path. The vector does not include the program name itself; the package adds it. This exactness holds on Linux and macOS; see the note above for Windows.
Exit code, output and error output
A program reports success with exit code zero and anything else on failure, and it writes its complaints to the error output. After Wait, read both: ReadStdout for the results and ReadStderr for the reason it failed. Call Cleanup after reading, because the captured output is kept in temporary files until then.
import os.process
func main:
mut args := Vector {} as Vector<str>
defer args.Free()
args.Push("-c")
args.Push("echo copying files; echo disk is full >&2; exit 3")
mut p := process.StartCaptured("sh", args)
code := process.Wait(p)
out := process.ReadStdout(p).Trim()
err := process.ReadStderr(p).Trim()
process.Cleanup(p)
println "output:", out
if code != 0:
println "failed with code", code, "and said:", err
ret 0
end
println "all good"
endRun several programs at once
StartCaptured returns as soon as the program has started, so you can start several and wait for them afterwards. The total time is about that of the slowest one rather than the sum of all. Waiting in the order you started them keeps the output in a fixed order, even when a later job finished first.
import os.process
func job(name: str, seconds: str): process.Process:
mut args := Vector {} as Vector<str>
defer args.Free()
args.Push("-c")
args.Push("sleep " + seconds + "; echo " + name + " done")
ret process.StartCaptured("sh", args)
end
func main:
mut jobs := Vector {} as Vector<process.Process>
defer jobs.Free()
jobs.Push(job("thumbnails", "0.3"))
jobs.Push(job("search index", "0.1"))
jobs.Push(job("sitemap", "0.2"))
println "started", jobs.Len(), "jobs"
repeat jobs.Len() in i:
mut p := jobs.Get(i)
code := process.Wait(p)
println code, process.ReadStdout(p).Trim()
process.Cleanup(p)
end
endKeep the number of programs you run at once reasonable; os.platform's CpuCount is a good limit for work that keeps the processor busy.
A helper that runs a program and returns its output
Most programs need the same few lines every time: start, check that it started, wait, read, clean up. Put them in one function. A defer right after the start makes sure Cleanup runs on every way out of the function, and an out-parameter tells the caller whether the program succeeded.
import os.error
import os.process
func capture(program: str, args: Vector<str>, ok &: bool): str:
error.Clear()
mut p := process.StartCaptured(program, args)
if error.HasError():
ok = false
ret ""
end
defer process.Cleanup(p)
ok = process.Wait(p) == 0
ret process.ReadStdout(p)
end
func main:
mut args := Vector {} as Vector<str>
defer args.Free()
args.Push("-c")
args.Push("printf 'pear\\napple\\nfig\\n' | sort")
mut ok := false
out := capture("sh", args, ok)
println "worked:", ok
print out
capture("/no/such/tool", args, ok)
println "missing tool worked:", ok
endStop a program
Terminate sends SIGTERM, which a well-behaved program catches to save its work and exit; Kill sends SIGKILL, which cannot be caught. Either way, Wait collects the program afterwards. A program ended by a signal reports 128 plus the signal number as its exit code, so 143 means SIGTERM and 137 means SIGKILL, the same numbers a shell shows.
import os.process
func main:
mut args := Vector {} as Vector<str>
defer args.Free()
args.Push("30")
mut worker := process.Start("sleep", args)
println "started, running:", process.IsRunning(worker)
if process.Terminate(worker) != 0:
println "could not signal it"
ret 1
end
code := process.Wait(worker)
println "stopped with exit code", code, "= 128 + signal", code - 128
println "running:", process.IsRunning(worker)
ret 0
endTalk to a running child
Start and StartCaptured run a program to the end. When you need to talk to it while it runs, describe it with a Command (NewCommand gives one for a program), pass it to Launch, and use the Child it returns. Set stdin_mode, stdout_mode and stderr_mode to Pipe (or call Piped) to connect a stream to your program, to Null to throw it away, or leave Inherit to share your own. Merge on the error stream puts it into the output pipe in the order it was written. Arg, Env, Unset and the cwd and clear_env fields set the arguments, the environment and the folder. Writing to a child that has already gone returns false and never kills your program.
import os.process
func main:
mut cmd := process.NewCommand("sh")
cmd.Arg("-c")
cmd.Arg("read name; echo hello $name")
cmd.stdin_mode = process.Pipe
cmd.stdout_mode = process.Pipe
mut child := process.Launch(cmd)
if not child.ok:
println "could not start:", child.error
ret 1
end
child.Write("Sara\n")
reply := child.ReadLine(2000)
println reply.data
reply.Free()
println "exit code:", child.Wait(2000).code
child.Close()
cmd.Free()
endRead with a time limit
Every read takes a time in milliseconds: 0 only looks, a positive number waits that long, a negative number waits for ever. ReadAvailable returns whatever has arrived, ReadLine one complete line, Read an exact number of bytes, ReadUntil everything up to a given text (the way to wait for a prompt) and ReadFor everything that arrives in a period. A read returns a Chunk: check timed_out (nothing came, and the child may still be working) and eof (the stream ended) before using data, and Free it. Bytes that were read but not yet returned stay in the child, so a timed-out call loses nothing. The ReadErr methods do the same for the error pipe, and ReadBytes reads binary data into a buffer.
import os.process
func main:
mut cmd := process.NewCommand("sh")
cmd.Arg("-c")
cmd.Arg("echo first; sleep 0.3; echo second")
cmd.stdout_mode = process.Pipe
mut child := process.Launch(cmd)
a := child.ReadLine(2000)
println "line:", a.data
b := child.ReadLine(50)
println "nothing yet:", b.timed_out
c := child.ReadLine(2000)
println "line:", c.data
d := child.ReadLine(2000)
println "closed:", d.eof
a.Free()
b.Free()
c.Free()
d.Free()
_status := child.Wait(2000)
child.Close()
cmd.Free()
endExit code or signal
Child.Wait takes a time limit and returns a Status. A child that ended by itself has exited set and its code in code; one that a signal ended has signaled set and the number in signal. This differs from Process.Wait, which folds a signal into 128 plus the number. ReturnCode gives the Python-style number (the code, or the negative signal), and Success is true for code 0. When the limit passes first, timed_out is set and the child keeps running; Poll asks whether it is still running without waiting.
import os.process
func describe(script: str):
mut cmd := process.NewCommand("sh")
cmd.Arg("-c")
cmd.Arg(script)
mut child := process.Launch(cmd)
st := child.Wait(2000)
if st.exited:
println "exited with code", st.code
else st.signaled:
println "killed by signal", st.signal
end
child.Close()
cmd.Free()
end
func main:
describe("exit 3")
describe("kill -TERM $$")
describe("kill -KILL $$")
endRun with a timeout
RunTimeout is the one-call version: it writes the input text, collects the whole output and error output, and kills the child when the time runs out. It reads both pipes while it writes, so a child that prints a megabyte on each stream cannot deadlock it. RunCommand does the same for a Command with an environment or folder. The result is a RunResult: started, status, stdout, stderr and timed_out; call Free on it.
import os.process
func main:
mut args := Vector {} as Vector<str>
defer args.Free()
args.Push("-c")
args.Push("cat; echo done")
mut ok := process.RunTimeout("sh", args, "from stdin\n", 2000)
print ok.stdout
println "timed out:", ok.timed_out
ok.Free()
mut hang_args := Vector {} as Vector<str>
defer hang_args.Free()
hang_args.Push("-c")
hang_args.Push("sleep 30")
mut hang := process.RunTimeout("sh", hang_args, "", 200)
println "timed out:", hang.timed_out, "return code:", hang.status.ReturnCode()
hang.Free()
endStop a child and everything it started
Kill a shell and the programs it started keep running. Start the child with new_group and KillGroup (or SignalGroup with any signal) stops the whole group. RunTimeout does this for you. Signal, Kill and Terminate reach only the child itself.
import os.process
func main:
mut cmd := process.NewCommand("sh")
cmd.Arg("-c")
cmd.Arg("sleep 30 & sleep 30 & wait")
cmd.new_group = true
cmd.stdout_mode = process.Pipe
mut child := process.Launch(cmd)
println "group killed:", child.KillGroup()
st := child.Wait(2000)
println "signal:", st.signal
tail := child.ReadAvailable(2000)
println "pipe closed:", tail.eof
tail.Free()
child.Close()
cmd.Free()
endOn Unix a program that cannot be found, or a folder that cannot be entered, makes Launch return a child whose ok is false and whose error says why; the check happens before Launch returns, so there is no need to wait for an exit code 127.
On Windows Launch uses CreateProcessW and anonymous pipes. Every signal ends the child at once and is reported as a signal, RunCommand writes all of its input before it starts reading, and the pseudo terminal of os.pty is not available. The tests that start real children run on Linux and macOS.
| Need | Use |
|---|---|
| Run a program and see its output on the screen | Start, then Wait |
| Run a program and use its output | StartCaptured, Wait, ReadStdout, Cleanup |
| Stop a program politely, or at once | Terminate, Kill |
| Pipes or redirection | run sh with -c and a script, as in the examples above |
| A one-line command and its result | os.shell |
| Talk to a program while it runs | NewCommand, Launch, then Write and the Read methods |
| Wait for a prompt or a line, with a time limit | ReadUntil, ReadLine |
| Tell an exit code from a signal | Child.Wait and its Status |
| Run, capture everything, give up after a time | RunTimeout |
| Stop a program and everything it started | new_group and KillGroup |
| A program that needs a terminal | os.pty |
Types
Command
struct CommandLaunch, and call Free when done.| Name | Type | Description |
|---|---|---|
program | str = "" | The program to run. A name without a slash is searched on PATH. |
args | Vector | The arguments, not including the program name. Each reaches the child as one argument. |
cwd | str = "" | The folder to run in. Empty means your own current folder. |
env | HashMap | Variables to add to the child's environment, replacing any with the same name. |
unset_env | Vector | Names of variables to remove from the child's environment. |
clear_env | bool = false | When true the child starts with an empty environment plus only the variables in env. |
stdin_mode | int = 0 | What to do with the child's input: Inherit, Pipe or Null. |
stdout_mode | int = 0 | What to do with the child's output: Inherit, Pipe or Null. |
stderr_mode | int = 0 | What to do with the child's error output: Inherit, Pipe, Null or Merge. |
new_group | bool = false | Starts the child in its own process group, so KillGroup can stop it together with everything it started. Unix only; on Windows it adds a new console process group. |
import os.process
func main:
mut cmd := process.NewCommand("sh")
cmd.Arg("-c")
cmd.Arg("echo $GREETING from $(pwd); echo trouble 1>&2")
cmd.Env("GREETING", "salam")
cmd.cwd = "/"
cmd.stdout_mode = process.Pipe
cmd.stderr_mode = process.Merge
mut child := process.Launch(cmd)
all := child.ReadFor(2000)
print all.data
all.Free()
println "exit:", child.Wait(2000).code
child.Close()
cmd.Free()
endArg
func Arg(a: str)The example for this item is shown with the ones above: os-process-Command.salam
Env
func Env(name: str, value: str)The example for this item is shown with the ones above: os-process-Command.salam
Unset
func Unset(name: str)The example for this item is shown with the ones above: os-process-Command.salam
Piped
func Piped()Pipe.import os.process
func main:
mut cmd := process.NewCommand("cat")
cmd.Piped()
mut child := process.Launch(cmd)
child.Write("hello child\n")
line := child.ReadLine(2000)
println "got:", line.data
line.Free()
child.CloseStdin()
status := child.Wait(2000)
println "exit code:", status.code
child.Close()
cmd.Free()
endFree
func Free()The example for this item is shown with the ones above: os-process-Launch.salam
Status
struct Status| Name | Type | Description |
|---|---|---|
exited | bool = false | True when the child ended by itself and code holds its exit code. |
code | int = 0 | The exit code, valid when exited is true. |
signaled | bool = false | True when a signal ended the child and signal holds its number. |
signal | int = 0 | The number of the signal that ended the child, valid when signaled is true. |
timed_out | bool = false | True when a wait gave up before the child ended; the child is then still running. |
import os.process
func main:
mut cmd := process.NewCommand("sleep")
cmd.Arg("30")
mut child := process.Launch(cmd)
println "running:", child.Poll()
println "sent:", child.Terminate()
status := child.Wait(2000)
println "signaled:", status.signaled, "signal:", status.signal
println "return code:", status.ReturnCode()
println "running:", child.Poll()
child.Close()
cmd.Free()
endSuccess
func Success(): boolimport os.process
func main:
mut cmd := process.NewCommand("sh")
cmd.Arg("-c")
cmd.Arg("printf 'abcdef'; sleep 0.2; printf 'ghi:tail\\n'")
cmd.stdout_mode = process.Pipe
mut child := process.Launch(cmd)
first := child.Read(4, 2000)
println "four bytes:", first.data
upto := child.ReadUntil(":", 2000)
println "up to the colon:", upto.data
rest := child.ReadLine(2000)
println "rest of the line:", rest.data
end_of := child.ReadAvailable(2000)
println "ended:", end_of.eof
first.Free()
upto.Free()
rest.Free()
end_of.Free()
status := child.Wait(2000)
println "success:", status.Success()
child.Close()
cmd.Free()
endReturnCode
func ReturnCode(): intThe example for this item is shown with the ones above: os-process-Signal.salam
Chunk
struct Chunk| Name | Type | Description |
|---|---|---|
data | str = "" | The text read, as a string that stops at the first zero byte. Use len for the real byte count. Empty (but still yours to free) on a timeout or at the end. |
len | int = 0 | The number of bytes read. For a line it excludes the line ending. |
eof | bool = false | True when the stream has ended and no more data will come. |
timed_out | bool = false | True when nothing arrived within the time allowed. Whatever was partly read stays buffered for the next call. |
The example for this item is shown with the ones above: os-process-Read.salam
Free
func Free()The example for this item is shown with the ones above: os-process-Launch.salam
RunResult
struct RunResultRunTimeout collected: whether the child started, how it ended and all of its output and error output.| Name | Type | Description |
|---|---|---|
started | bool = false | False when the program could not be started; the other fields are then empty. |
status | Status | How the child ended. After a timeout it is the kill signal with timed_out set. |
stdout | str = "" | All the output, as a string that stops at the first zero byte. |
stderr | str = "" | All the error output. |
timed_out | bool = false | True when the time ran out and the child was killed. |
import os.process
func main:
mut args := Vector {} as Vector<str>
defer args.Free()
args.Push("-c")
args.Push("echo fast output; echo warning 1>&2; exit 2")
mut quick := process.RunTimeout("sh", args, "", 2000)
println "code:", quick.status.code
print "stdout:", quick.stdout
print "stderr:", quick.stderr
quick.Free()
mut slow_args := Vector {} as Vector<str>
defer slow_args.Free()
slow_args.Push("-c")
slow_args.Push("echo started; sleep 30")
mut slow := process.RunTimeout("sh", slow_args, "", 300)
println "timed out:", slow.timed_out, "signal:", slow.status.signal
print "partial:", slow.stdout
slow.Free()
endFree
func Free()The example for this item is shown with the ones above: os-process-RunTimeout.salam
Child
struct ChildLaunch; call Close when you are done with it.| Name | Type | Description |
|---|---|---|
pid | int = 0 | The process id. |
ok | bool = false | True when the child started. When false, error says why. |
error | str = "" | Why the child could not start, or empty. |
finished | bool = false | True once a wait or a poll has seen the child end; status then holds the result. |
status | Status | How the child ended, valid once finished is true. |
h_in | i64 = -1 | The write end of the child's input pipe, or -1. Low level; the methods use it for you. |
h_out | i64 = -1 | The read end of the child's output pipe, or -1. On a terminal from os.pty this is the terminal itself. |
h_err | i64 = -1 | The read end of the child's error pipe, or -1. |
h_proc | i64 = -1 | The process handle on Windows, or -1. |
sent_signal | int = 0 | The last signal sent with Signal; Windows uses it to report a kill as a signal. |
out_buf | fdio.Buf | Bytes read from the output pipe but not yet returned. |
err_buf | fdio.Buf | Bytes read from the error pipe but not yet returned. |
The example for this item is shown with the ones above: os-process-Launch.salam
Write
func Write(text: str): boolThe example for this item is shown with the ones above: os-process-Launch.salam
WriteBytes
func WriteBytes(data: void*, n: int): boolThe example for this item is shown with the ones above: os-process-Launch.salam
CloseStdin
func CloseStdin()cat sees the end of its input. On a terminal it sends Ctrl-D instead.The example for this item is shown with the ones above: os-process-Launch.salam
ReadAvailable
func ReadAvailable(ms: int): Chunkms milliseconds for the first bytes. 0 only checks; a negative number waits for ever.The example for this item is shown with the ones above: os-process-Read.salam
ReadLine
func ReadLine(ms: int): Chunkms milliseconds for it to be complete. At the end of the stream it returns what is left with eof set.The example for this item is shown with the ones above: os-process-Read.salam
Read
func Read(n: int, ms: int): Chunkn bytes, waiting up to ms milliseconds in all. At the end of the stream it returns the fewer that were left with eof set.The example for this item is shown with the ones above: os-process-Read.salam
ReadUntil
func ReadUntil(text: str, ms: int): Chunkms milliseconds. This is how to wait for a prompt.The example for this item is shown with the ones above: os-process-Read.salam
ReadFor
func ReadFor(ms: int): Chunkms milliseconds, or until the stream ends. A negative number reads to the end.The example for this item is shown with the ones above: os-process-Command.salam
ReadBytes
func ReadBytes(dst: void*, max: int, ms: int): intmax bytes into a buffer you own. Returns the count, 0 on a timeout and -1 at the end of the stream.The example for this item is shown with the ones above: os-process-Launch.salam
ReadErrAvailable
func ReadErrAvailable(ms: int): ChunkReadAvailable, for the error pipe.The example for this item is shown with the ones above: os-process-Command.salam
ReadErrLine
func ReadErrLine(ms: int): ChunkReadLine, for the error pipe.The example for this item is shown with the ones above: os-process-Command.salam
ReadErrUntil
func ReadErrUntil(text: str, ms: int): ChunkReadUntil, for the error pipe.The example for this item is shown with the ones above: os-process-Command.salam
ReadErrFor
func ReadErrFor(ms: int): ChunkReadFor, for the error pipe.The example for this item is shown with the ones above: os-process-Command.salam
Wait
func Wait(ms: int): Statusms milliseconds for the child to end and returns its Status. On a timeout the status has timed_out set and the child keeps running. 0 only checks; a negative number waits for ever. A finished child returns the same status every time.The example for this item is shown with the ones above: os-process-Launch.salam
Poll
func Poll(): boolThe example for this item is shown with the ones above: os-process-Signal.salam
Signal
func Signal(sig: int): boolThe example for this item is shown with the ones above: os-process-Signal.salam
Kill
func Kill(): boolSigKill. Call Wait afterwards.The example for this item is shown with the ones above: os-process-Signal.salam
Terminate
func Terminate(): boolSigTerm, asking the child to finish.The example for this item is shown with the ones above: os-process-Signal.salam
SignalGroup
func SignalGroup(sig: int): boolnew_group.The example for this item is shown with the ones above: os-process-Signal.salam
KillGroup
func KillGroup(): boolThe example for this item is shown with the ones above: os-process-Signal.salam
Close
func Close()The example for this item is shown with the ones above: os-process-Launch.salam
Process
struct Process| Name | Type | Description |
|---|---|---|
pid | int = 0 | The process id. |
handle | void* | The process handle on Windows. |
running | bool = false | Set by Start and cleared by Wait, Kill and Terminate; it is not updated when the program exits on its own. |
exit_code | int = 0 | The exit code, once Wait has returned; 128 plus the signal number when a signal ended it. |
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)
endSpawnOptions
struct SpawnOptionsSpawn.| Name | Type | Description |
|---|---|---|
path | str = "" | The program. |
args | Vector | Its arguments. |
raw_args | str = "" | Windows only: text added to the command line as it is. |
cwd | str = "" | The folder to run in. |
output | str = "" | A file that receives standard output and standard error. |
The example for this item is shown with the ones above: os-process-Start.salam
Constants and variables
Inherit
const Inherit: i32 = 0The example for this item is shown with the ones above: os-process-Command.salam
Pipe
const Pipe: i32 = 1Write; read its output with the Read methods.The example for this item is shown with the ones above: os-process-Launch.salam
Null
const Null: i32 = 2The example for this item is shown with the ones above: os-process-Command.salam
Merge
const Merge: i32 = 3The example for this item is shown with the ones above: os-process-Command.salam
SigHup
const SigHup: i32 = 1The example for this item is shown with the ones above: os-process-Signal.salam
SigInt
const SigInt: i32 = 2The example for this item is shown with the ones above: os-process-Signal.salam
SigQuit
const SigQuit: i32 = 3The example for this item is shown with the ones above: os-process-Signal.salam
SigKill
const SigKill: i32 = 9The example for this item is shown with the ones above: os-process-Signal.salam
SigTerm
const SigTerm: i32 = 15The example for this item is shown with the ones above: os-process-Signal.salam
Functions
Launch
func Launch(c: Command): Childok; when it is false, error and os.error say why.The example for this item is shown with the ones above: os-process-Launch.salam
NewCommand
func NewCommand(program: str): CommandThe example for this item is shown with the ones above: os-process-Launch.salam
RunCommand
func RunCommand(c: Command, text: str, timeout_ms: int): RunResulttimeout_ms passes. A negative time means no limit.The example for this item is shown with the ones above: os-process-Command.salam
RunTimeout
func RunTimeout(program: str, args: Vector<str>, text: str, timeout_ms: int): RunResultThe example for this item is shown with the ones above: os-process-RunTimeout.salam
Start
func Start(path: str, args: Vector<str>): ProcessPATH. When it cannot start, pid is 0 and os.error holds ProcessFailed.The example for this item is shown with the ones above: os-process-Start.salam
StartCaptured
func StartCaptured(path: str, args: Vector<str>): ProcessStart, but sends the program's output and error output to two temporary files for you to read after Wait, and gives it an empty standard input.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): intWait afterwards to collect it.The example for this item is shown with the ones above: os-process-Kill.salam
Terminate
func Terminate(p &: Process): intWait afterwards. On Windows it is the same as Kill.The example for this item is shown with the ones above: os-process-Kill.salam
ReadStdout
func ReadStdout(p: Process): strStart. The text stops at the first zero byte.The example for this item is shown with the ones above: os-process-Start.salam
ReadStderr
func ReadStderr(p: Process): strStart.The example for this item is shown with the ones above: os-process-Start.salam
Cleanup
func Cleanup(p &: Process)StartCaptured.The example for this item is shown with the ones above: os-process-Start.salam
Close
func Close(p &: Process)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): boolrunning flag: true from Start until Wait, Kill or Terminate. It does not ask the system, so it stays true after the program exits by itself.The example for this item is shown with the ones above: os-process-Start.salam
Spawn
func Spawn(o: SpawnOptions): ProcessThe example for this item is shown with the ones above: os-process-Start.salam
TryWait
func TryWait(p &: Process): boolexit_code is set. It never blocks.The example for this item is shown with the ones above: os-process-Start.salam
KillTree
func KillTree(p &: Process)The example for this item is shown with the ones above: os-process-Start.salam