Core\Process::run
Core\Process::run(
string $path,
array<string> $argv,
{workingDirectory?: string, env?: array<string>, timeout?: Core\Time\Duration},
): Core\Process\ResultRuns another program, waits until it ends, and returns its exit code and everything it printed. The printed output is tainted bytes, because another program wrote it and it can be anything. Use as string for text.
You give the name or the path of the program and an array of arguments, one in each element. A name such as git is looked up in the folders of PATH. The program is started directly, never through a shell. So a space or a ; in an argument is only text, and it cannot start a second command.
A program you start from the command line may run any program. On a server, the process.exec capability in nvs.toml must name the program or its folder. Otherwise run throws a RuntimeError. A .bat, .cmd or .ps1 file always throws a RuntimeError. A program that cannot be started, for example because it is missing, throws an IOError.
The examples below run a program and read its output, show a program that is not allowed, and check whether a tool succeeded.
Parameters
$pathstringThe program to start, as an absolute path. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it.PATHis not searched, solsmeans a file namedlsin that folder.$argvarray<string>The arguments, one in each element:["-n", "1", $host]. An element with a space, a quote or a;in it is still one argument, on every platform.
Options
workingDirectorystringoptionalThe folder the program starts in, as an absolute path. A relative path must be written directly in the code, and is joined to the folder of this file. Without it, the program starts in the same folder as this program.envarray<string>optionalThe environment variables of the program, as name => value. This replaces all of them, so a variable that is not in the array is not set. Without it, the program gets only these variables from this program:PATH,HOMEorUSERPROFILE,TEMP,TMP,LANG, andSYSTEMROOTon Windows. Asecretvalue is a value the program must keep hidden, such as a password. CallCore\Secret::revealon it first.timeoutCore\Time\DurationoptionalThe longest time the program may run, counted from the start. When it passes, the program is stopped, and this call throws aTimeoutError.
Return value
Returns Core\Process\Result. A Core\Process\Result with the exit code and everything the program wrote to its output and to its error output. The program cannot write to this program's own output, so the two never mix.
Errors
RuntimeErrorprocess.execdoes not allow this program, or it is a.bat,.cmdor.ps1file. Those are not allowed on any platform, because Windows starts them through a shell. The error is also thrown when the program writes more than[limits] max_outputin total. Then the program is stopped and nothing is returned. It is also thrown whenworkingDirectoryis a relative path, or whentimeoutis zero or shorter. Anenvname that is empty or has a=in it throws it too.TimeoutErrorThe program was still running whentimeoutpassed. The program is stopped and nothing is returned.IOErrorThe program could not be started or waited for. For example, nothing is at the path, the file is not a program, or theworkingDirectoryfolder does not exist.
Changelog
| Version | Change |
|---|---|
0.0.6 | The option cwd of Core\Process::run and Core\Process::spawn is now workingDirectory. The option args of Core\Queue::push is now arguments. |
Examples
The examples below run with this configuration file.
# These lines allow only the two programs the examples run, `/bin/echo` and `/bin/ls`.# Without them, a program run with `nvs run` may run any program.[capabilities.process]exec = ["/bin/echo", "/bin/ls"]Running a program
<?nvs// Shows how to run another program and read what it prints.// requires: unix
// Each element of the array is one argument. The program is started directly, without a shell,// so the `;` and the spaces are only text.var $result = Core\Process::run('/bin/echo', ['Hello; rm -rf /', 'and goodbye']);
// `stdout()` returns `bytes`. `as string` converts them to text.echo $result->stdout() as string;
// `exitCode()` is `0` when the program succeeded. This prints `0`.echo $result->exitCode(), "\n";Hello; rm -rf / and goodbye0A program that is not allowed
<?nvs// Shows what happens when a program is not in the folders that `nvs.toml` allows.
// `nvs.toml` allows `/bin` and `/usr/bin`. `/opt/backup/bin/backup` is in neither folder.// So `Core\Process::run` throws a `RuntimeError`, and the program is not started.try { var $result = Core\Process::run('/opt/backup/bin/backup', ['--all']); echo 'The program ran and returned ', $result->exitCode(), ".\n";} catch (RuntimeError $notAllowed) { echo "The program was not started.\n";}The program was not started.A tool that can fail
<?nvs// Shows a small method that runs a tool, checks that it succeeded, and returns what it printed.// requires: unix
class Tools{ public static function run(string $path, array<string> $arguments): tainted string { var $result = Core\Process::run($path, $arguments); // A tool returns an exit code that is not `0` when it fails. // Then this method throws an error. if ($result->exitCode() != 0) { throw new RuntimeError('the tool failed'); } return $result->stdout() as string; }}
// `ls` lists the files in a folder. This folder exists, so the list is printed.echo Tools::run('/bin/ls', ['docs/examples/core/Process-Result']);
// This folder does not exist, so `ls` fails and `Tools::run` throws.try { echo Tools::run('/bin/ls', ['docs/no-such-folder']);} catch (RuntimeError $failed) { echo $failed->message, "\n";}exitCodestderrstdoutthe tool failed