Skip to content

Command Execution

Execute external commands and shell scripts with full control over I/O streams.

For executor configuration, see Executor.

local exec = require("exec")

Get a process executor resource by ID:

local executor, err = exec.get("app:exec")
if err then
return nil, err
end
-- Use executor
local proc = executor:exec("ls -la")
-- ...
-- Release when done
executor:release()
ParameterTypeDescription
idstringResource ID

Returns: Executor, error

Create a new process with the specified command:

-- Simple command
local proc, err = executor:exec("echo 'Hello, World!'")
-- With working directory
local proc = executor:exec("npm install", {
work_dir = "/app/project"
})
-- With environment variables
local proc = executor:exec("python script.py", {
work_dir = "/scripts",
env = {
PYTHONPATH = "/app/lib",
DEBUG = "true",
API_KEY = api_key
}
})
-- Run shell script
local proc = executor:exec("./deploy.sh production", {
work_dir = "/app/scripts",
env = {
DEPLOY_ENV = "production"
}
})
ParameterTypeDescription
cmdstringCommand to execute
options.work_dirstringWorking directory
options.envtableEnvironment variables

Returns: Process, error

Start the process and wait for completion.

local proc = executor:exec("./build.sh")
local ok, err = proc:start()
if err then
return nil, err
end
local exit_code, err = proc:wait()
if err then
return nil, err
end
if exit_code ~= 0 then
return nil, errors.new("INTERNAL", "Build failed with exit code: " .. exit_code)
end

Get streams to read process output.

local proc = executor:exec("./process-data.sh")
local stdout = proc:stdout_stream()
local stderr = proc:stderr_stream()
proc:start()
-- Read all stdout
local output = {}
while true do
local chunk = stdout:read(4096)
if not chunk then break end
table.insert(output, chunk)
end
local result = table.concat(output)
-- Check for errors
local err_output = {}
while true do
local chunk = stderr:read(4096)
if not chunk then break end
table.insert(err_output, chunk)
end
local exit_code = proc:wait()
stdout:close()
stderr:close()
if exit_code ~= 0 then
return nil, errors.new("INTERNAL", table.concat(err_output))
end
return result

Write data to process stdin.

-- Pipe data to command
local proc = executor:exec("sort")
local stdout = proc:stdout_stream()
proc:start()
-- Write input
proc:write_stdin("banana\napple\ncherry\n")
proc:write_stdin("") -- Signal EOF
-- Read sorted output
local sorted = stdout:read()
print(sorted) -- "apple\nbanana\ncherry\n"
proc:wait()
stdout:close()

Send signals or close the process.

local proc = executor:exec("./long-running-server.sh")
proc:start()
-- ... later, need to stop it ...
-- Graceful shutdown (SIGTERM)
proc:close()
-- Or force kill (SIGKILL)
proc:close(true)
-- Or send specific signal
local SIGINT = 2
proc:signal(SIGINT)

Exec operations are subject to security policy evaluation.

ActionResourceDescription
exec.getExecutor IDAcquire an executor resource
exec.runCommandExecute a specific command
ConditionKindRetryable
Invalid IDerrors.INVALIDno
Permission deniederrors.INVALIDno
Process closederrors.INVALIDno
Process not startederrors.INVALIDno
Already startederrors.INVALIDno

See Error Handling for working with errors.