Module eco.sys
System and process related utilities.
Functions
| exec (cmd[, env]) | Execute a program. |
| sh (cmd[, timeout=30]) | Execute a shell command and return stdout and stderr. |
| spawn (f) | Spawn a new process to run a Lua function. |
| signal (signum, cb, Additional) | Listen for a specific signal and invoke a callback asynchronously. |
| uptime () | Get system uptime. |
| waitpid (pid) | Wait for a child process status change (non-blocking). |
| getpid () | Get current process id. |
| getppid () | Get parent process id. |
| getpwnam (name) | Lookup a user by name. |
| kill (pid, sig) | Send a signal to a process. |
| fork () | Fork the current process. |
| prctl (option[, arg]) | Process control operations. |
| get_nprocs () | Get number of available processors. |
| strerror (errno) | Convert errno value to message. |
Fields
| SIGINT | Signal number for interrupt from keyboard. |
| SIGTERM | Signal number for termination. |
| SIGHUP | Signal number for terminal line hangup. |
Class process
| process:close () | Close stdout and stderr file descriptors. |
| process:wait ([timeout]) | Wait for the process to exit. |
| process:signal (sig) | Send a signal to the process. |
| process:kill () | Force kill the process with SIGKILL. |
| process:stop ([timeout=3]) | Stop the process gracefully, then force kill if needed. |
| process:read_stdout () | Read from process stdout. |
| process:read_stderr () | Read from process stderr. |
Class signal
| signal:close () | Stop listening for the signal and release resources. |
Functions
- exec (cmd[, env])
-
Execute a program.
Spawns a new process and returns a process handle object. The returned object provides methods to wait for process exit, read stdout/stderr.
There are two supported calling forms:
Argument list form:
p, err = sys.exec(cmd, arg1, arg2, ...)Table form with optional environment:
p, err = sys.exec({ cmd, arg1, arg2, ... }, env)
In the second form,
envis a table of environment variables.Parameters:
- cmd
string or table
- a string representing the program to execute, followed by arguments
- or a table
{ cmd, arg1, arg2, ... }
- env table Environment variables table (only valid when first argument is a table form). (optional)
Returns:
-
process
Process handle object on success.
Or
- nil On failure.
- string Error message when failed.
Usage:
-- simple form local p, err = sys.exec('ls', '-l', '/tmp') if not p then print(err) end -- table form with environment local p, err = sys.exec({ 'printenv', 'A' }, { A = '123' })
- sh (cmd[, timeout=30])
-
Execute a shell command and return stdout and stderr.
This is a convenience wrapper around exec. It accepts a command string or a table of arguments. If a string is provided, it will be executed using
/bin/sh -c.Parameters:
- cmd
string or table
Command to execute.
- If string, executed via
/bin/sh -c. - If table, first element is program, remaining are arguments.
- If string, executed via
- timeout number Timeout in seconds for reading stdout/stderr. (default 30)
Returns:
Or
- nil On failure.
- string Error message when failed.
Usage:
local out, err = sys.sh({ 'echo', 'hello' }) if not out then print("Command failed:", e) else print("stdout:", out) print("stderr:", err) end -- Table form with program and arguments local out, err, e = sys.sh({ "ls", "-l", "/tmp" })
- cmd
string or table
- spawn (f)
-
Spawn a new process to run a Lua function.
This function forks a child process and runs the given function
finside it. The child process will be terminated if the parent dies (PR_SET_PDEATHSIG = SIGKILL).Parameters:
- f function Lua function to run in the child process.
Returns:
-
int
Child process ID (in the parent process).
Or
- nil On failure.
- string Error message.
Usage:
local pid, err = sys.spawn(function() print("Hello from child process!") end) if not pid then print("Failed to spawn child:", err) else print("Spawned child PID:", pid) end
- signal (signum, cb, Additional)
-
Listen for a specific signal and invoke a callback asynchronously.
This function wraps
signalfdand spawns a coroutine to continuously read the signal. When the specified signalsigis received, the callbackcbis invoked with any additional arguments.The returned signal object controls the listener lifetime. Call
sig:close()to stop listening.Internally this function blocks
signumin the process mask and usessignalfdfor delivery. The signal will be unblocked when the listener is closed.Note:
SIGCHLDis reserved by eco's scheduler for child process reaping and must not be listened to via this API.Parameters:
- signum int Signal number to listen for (e.g., sys.SIGINT).
- cb function Callback function to invoke when the signal occurs.
- Additional ... arguments passed to the callback.
Returns:
-
signal
Signal object (use signal:close to stop listening).
Or
- nil On failure.
- string Error message.
Usage:
local sig sig, err = sys.signal(sys.SIGINT, function() print('Received SIGINT!') sig:close() end) if not sig then print('Failed to listen for signal:', err) end
- uptime ()
-
Get system uptime.
Returns:
-
int
Uptime in seconds.
- waitpid (pid)
-
Wait for a child process status change (non-blocking).
This is a thin wrapper around
waitpid(pid, ..., WNOHANG | WUNTRACED).Parameters:
- pid
int
Process id to wait for. Use
-1for any child.
Returns:
-
int
pid The pid that changed state, or
0if none. - table Status information.
Or
- nil On error.
- string err Error message.
- pid
int
Process id to wait for. Use
- getpid ()
-
Get current process id.
Returns:
-
int
pid
- getppid ()
-
Get parent process id.
Returns:
-
int
pid
- getpwnam (name)
-
Lookup a user by name.
Parameters:
- name string User name.
Returns:
-
table
User info table with fields:
username,password,uid,gid,home,shell.Or
- nil On error or not found.
- string err Error message.
- kill (pid, sig)
-
Send a signal to a process.
Parameters:
- pid int Target pid.
- sig int Signal number.
Returns:
-
boolean
true on Success
Or
- nil On error
- string err Error message.
- fork ()
-
Fork the current process.
Returns:
-
int
Child pid in parent, 0 in child.
Or
- nil On error.
- string Error message.
- prctl (option[, arg])
-
Process control operations.
Currently supports
PR_SET_PDEATHSIG.Parameters:
- option int prctl option.
- arg int Option-specific argument. (optional)
Returns:
-
boolean
true on success.
Or
- nil On error or unsupported option.
- string Error message.
- get_nprocs ()
-
Get number of available processors.
Returns:
-
int
n
- strerror (errno)
-
Convert errno value to message.
Parameters:
- errno int Error number.
Returns:
-
string
message
Fields
- SIGINT
- Signal number for interrupt from keyboard.
- SIGTERM
- Signal number for termination.
- SIGHUP
- Signal number for terminal line hangup.
Class process
This object represents a running child process.
- process:close ()
- Close stdout and stderr file descriptors.
- process:wait ([timeout])
-
Wait for the process to exit.
If the child has already exited before calling this method, it returns immediately with the status cached on this process handle.
Parameters:
- timeout number Timeout in seconds. (optional)
Returns:
- int pid Process id.
- int status Exit status.
Or
- nil On timeout or error.
- string Error message.
- process:signal (sig)
-
Send a signal to the process.
Parameters:
- sig int Signal number.
Returns:
-
boolean
true On success.
Or
- nil On failure.
- string Error message.
- process:kill ()
-
Force kill the process with SIGKILL.
Returns:
-
boolean
true On success.
Or
- nil On failure.
- string Error message.
- process:stop ([timeout=3])
-
Stop the process gracefully, then force kill if needed.
This sends SIGTERM first, waits up to
timeoutseconds, then sends SIGKILL if the process is still alive. Waiting remains coroutine-based.Parameters:
- timeout number Wait timeout in seconds for each phase. (default 3)
Returns:
- int pid Process id.
- table status Exit status table.
Or
- nil On error.
- string Error message.
- process:read_stdout ()
-
Read from process stdout.
See read
- process:read_stderr ()
-
Read from process stderr.
See read
Class signal
This object represents one signal listener created by sys.signal.
Call signal:close to stop listening and release underlying resources. The close operation is idempotent.