ChildProcess

A program you started, live in the tree: its output line by line, its input, its exit code.

ProcessService:StartAsync gives one, a child of ProcessService named like python 1234. Connect Line right after the start. A ChildProcess whose Line or Exited you connect, or that you wait for, keeps the run going until it exits. KillAsync ends it and everything it started, and Destroy() kills it too. A program that buffers its output, such as python without -u, delivers it late.

ProcessService:StartAsync(program, args) gives one.

Example

Talk to a programShotScript
-- Starts a program, hears its lines, writes to its input, closes the input and waits for the exit code.
use ProcessService
local SCRIPT = "import sys\nfor line in sys.stdin:\n    print('got ' + line.strip())\nsys.exit(3)"
local child, why = ProcessService:StartAsync("python", { "-u", "-c", SCRIPT })
if not child then
	error("couldn't start python: " .. tostring(why))
end
child.Line:Connect(function(line : string, stream : string)
	print("[" .. stream .. "] " .. line)
end)
child.Exited:Connect(function(code : number)
	print("Exited fired with " .. code)
end)
print("running: Pid set " .. tostring(child.Pid ~= nil) .. ", ExitCode " .. tostring(child.ExitCode))
child:Write("buy milk\n")
child:Write("call home\n")
child:CloseInput()
local code = child:WaitAsync()
print("WaitAsync gave " .. code .. ", ExitCode " .. tostring(child.ExitCode))
What it printedOutput
running: Pid set true, ExitCode nil
[stdout] got buy milk
[stdout] got call home
Exited fired with 3
WaitAsync gave 3, ExitCode 3

Properties

NameTypeDefaultDescription
ExitCodenumber?read-onlyThe exit code once the program has ended, nil while it runs; 1 after a kill.
Pidnumber?read-onlyThe program's process id, nil only when it ended before StartAsync could see it.

Methods

NameArgumentsReturnsDescription
CloseInput()()Closes the program's input without waiting; text written after it is dropped.
KillAsync
yields
()()Waits until the program and everything it started are gone.
WaitAsync
yields
()numberWaits for the program to end and gives its exit code, at once when it has ended already.
Write(text: string)()Sends text to the program's input as it is, without waiting; writes go in order, and ones after CloseInput or the end are dropped.

Events

NamePassesDescription
Exited(number)Fires once with the exit code, after the program ended and its last line was delivered.
Line(string, ProcessStream)Fires per output line with the text, line end dropped, and its stream, "stdout" or "stderr"; connecting it keeps the run alive until it exits.
From ShotInstance 33