ProcessService

Start other programs from yours: stream their output line by line, write to their input, wait for them, and kill them with everything they started.

StartAsync starts a program with no window of its own, at below-normal priority, and gives a ChildProcess; RunAsync runs one to its end and gives its output. A program is a path or a name found on PATH. The first start in a run takes a little longer than later ones. When the run ends, every child still running is killed. See process service.

ShotInstanceservicenot creatablePrograms

A service: write use ProcessService.

Example

Run, list and killShotScript
-- Runs a short command to its end, then starts a long one, finds it in the process list and kills it.
use ProcessService
local result, why = ProcessService:RunAsync("python", { "-c", "import sys; print(sys.stdin.read().upper())" }, nil, "buy milk")
if not result then
	error("couldn't run python: " .. tostring(why))
end
print("RunAsync: code " .. result.Code .. ", stdout " .. result.Stdout)

local child, startWhy = ProcessService:StartAsync("python", { "-c", "import time; time.sleep(30)" })
if not child or not child.Pid then
	error("couldn't start python: " .. tostring(startWhy))
end
local processes : {ProcessInfo} = ProcessService:ListAsync() or {}
local listed = false
for _, info in processes do
	listed = listed or info.Pid == child.Pid
end
print("StartAsync's program is in ListAsync: " .. tostring(listed))
local ended = ProcessService:KillAsync(child.Pid)
print("KillAsync ended " .. tostring(ended) .. " process, its exit code " .. child:WaitAsync())
What it printedOutput
RunAsync: code 0, stdout BUY MILK
StartAsync's program is in ListAsync: true
KillAsync ended 1 process, its exit code 1

Methods

NameArgumentsReturnsDescription
KillAsync
yields
(pid: number)(number?, string?)Waits until the process and everything it started have ended, giving how many ended; 0 when no process has that pid.
ListAsync
yields
()({ ProcessInfo }?, string?)Waits for every process on the computer, each with Pid, ParentPid, Name and CommandLine, which is nil for system processes.
RunAsync
yields
(program: string, args: { string }?, options: ProcessStartOptions?, input: string?)(ProcessRunResult?, string?)Waits for the program to end after writing input, giving Code, Stdout and Stderr, each stream's lines joined by newlines.
StartAsync
yields
(program: string, args: { string }?, options: ProcessStartOptions?)(ChildProcess?, string?)Waits until the program runs, with no window and at below-normal priority; connect Line right away, since lines start arriving then.
From ShotInstance 33