Process Module
The process module provides a function to execute operating system commands from Lua scripts. This function is useful for automation of administrative tasks, gathering system information, and integration with external tools.
-
Execution of commands with a non-privileged user
-
Capture stdout and stderr separately
-
Structured return with success/error
-
Support for variable arguments
Security note: All commands are executed as the monstasb user to ensure security and permission control.
Available Functions
Section titled “Available Functions”1. process.exec(command, [arg1], [arg2], ...)
Section titled “1. process.exec(command, [arg1], [arg2], ...)”Executes an operating system command with the specified arguments.
Parameters:
Section titled “Parameters:”-
command (string): Name of the command/program to execute
-
… (optional, strings): Additional arguments for the command
Return:
Section titled “Return:”-
tuple:
(out, err)where:-
out (string or nil): Standard output of the command if successful, or
nilif it fails -
err (string or nil): Error output of the command if it fails, or
nilif successful
-
Behavior:
Section titled “Behavior:”-
The command is executed as the
monstasbuser -
If the command returns exit code 0 (success):
-
outcontains the command output -
errisnil
-
-
If the command fails (code ≠ 0):
-
outis nil -
errcontains the command’s error output (stderr)
-
-
If there is an execution error (command not found, etc.):
-
outisnil -
errcontains the error message
-
Example Usage:
Section titled “Example Usage:”-- Execute simple commandlocal saida, erro = process.exec("ls", "-la", "/tmp")if saida then print("Conteúdo de /tmp:") print(saida)else print("Erro:", erro)end
-- Execute command with multiple argumentslocal saida, erro = process.exec("df", "-h")if saida then -- Process df output local linhas = string.split(saida, "\n") for _, linha in ipairs(linhas) do print("Linha:", linha) endend
-- Check if a service is runninglocal saida, erro = process.exec("systemctl", "is-active", "nginx")if saida then local status = string.trim(saida) if status == "active" then print("Nginx está ativo") else print("Nginx não está ativo:", status) endelse print("Erro ao verificar Nginx:", erro)end
-- Collect system informationlocal comandos = { {"uname", "-a"}, {"uptime"}, {"free", "-h"}, {"df", "-h", "/"}}
for _, cmd_args in ipairs(comandos) do local comando = table.remove(cmd_args, 1) local saida, erro = process.exec(comando, table.unpack(cmd_args)) if saida then print("=== " .. comando .. " ===") print(saida) endendAdditional Information
Section titled “Additional Information”Execution as a Specific User
Section titled “Execution as a Specific User”All commands are executed as the monstasb user:
-- Security: commands are not executed as rootprocess.exec("whoami") -- Will return "monstasb", not "root"Support for Variable Arguments
Section titled “Support for Variable Arguments”Accepts any number of arguments:
-- 1 argumentprocess.exec("ls")
-- 2 argumentsprocess.exec("ls", "-la")
-- Multiple argumentsprocess.exec("find", ".", "-name", "*.log", "-type", "f", "-mtime", "+7")