Skip to main content
The ChildProcess class allows you to spawn and manage child processes from your NativePHP application. You can run system commands, PHP scripts, Node.js scripts, and Artisan commands as separate processes.

Available Methods

start()

Start a new child process with a custom command.
cmd
string|array
required
The command to execute. Can be a string or an array of command arguments.
alias
string
required
A unique identifier for this process. Used to reference the process later.
cwd
string|null
default:"base_path()"
The working directory for the process. Defaults to your application’s base path.
env
array|null
Environment variables to pass to the process.
persistent
bool
default:"false"
Whether the process should restart automatically if it exits.
return
ChildProcess
Returns the ChildProcess instance with populated properties.
use Native\Desktop\Facades\ChildProcess;

$process = ChildProcess::start(
    cmd: 'npm run dev',
    alias: 'vite',
    persistent: true
);

php()

Start a PHP script as a child process.
cmd
string|array
required
The PHP script or command to execute.
alias
string
required
A unique identifier for this process.
env
array|null
Environment variables to pass to the PHP process.
persistent
bool
default:"false"
Whether the process should restart automatically if it exits.
iniSettings
array|null
PHP ini settings to apply to the process (e.g., ['memory_limit' => '256M']).
return
ChildProcess
Returns the ChildProcess instance.
use Native\Desktop\Facades\ChildProcess;

$process = ChildProcess::php(
    cmd: 'script.php',
    alias: 'background-task',
    iniSettings: ['memory_limit' => '512M']
);

node()

Start a Node.js script as a child process.
cmd
string|array
required
The Node.js script or command to execute.
alias
string
required
A unique identifier for this process.
env
array|null
Environment variables to pass to the Node.js process.
persistent
bool
default:"false"
Whether the process should restart automatically if it exits.
return
ChildProcess
Returns the ChildProcess instance.
use Native\Desktop\Facades\ChildProcess;

$process = ChildProcess::node(
    cmd: 'server.js',
    alias: 'node-server',
    persistent: true
);

artisan()

Run an Artisan command as a child process.
cmd
string|array
required
The Artisan command to execute (without the ‘artisan’ prefix).
alias
string
required
A unique identifier for this process.
env
array|null
Environment variables to pass to the Artisan command.
persistent
bool
default:"false"
Whether the process should restart automatically if it exits.
iniSettings
array|null
PHP ini settings to apply to the process.
return
ChildProcess
Returns the ChildProcess instance.
use Native\Desktop\Facades\ChildProcess;

$process = ChildProcess::artisan(
    cmd: 'schedule:work',
    alias: 'scheduler',
    persistent: true
);

get()

Retrieve information about a running process by its alias.
alias
string|null
The alias of the process to retrieve. If called on an instance, defaults to that instance’s alias.
return
ChildProcess|null
Returns the ChildProcess instance if found, or null if not found.
use Native\Desktop\Facades\ChildProcess;

$process = ChildProcess::get('vite');

if ($process) {
    echo "Process ID: {$process->pid}";
}

all()

Retrieve all running child processes.
return
array
Returns an associative array of ChildProcess instances, keyed by their aliases.
use Native\Desktop\Facades\ChildProcess;

$processes = ChildProcess::all();

foreach ($processes as $alias => $process) {
    echo "{$alias}: PID {$process->pid}\n";
}

stop()

Stop a running child process.
alias
string|null
The alias of the process to stop. If called on an instance, defaults to that instance’s alias.
use Native\Desktop\Facades\ChildProcess;

ChildProcess::stop('vite');

restart()

Restart a running child process.
alias
string|null
The alias of the process to restart. If called on an instance, defaults to that instance’s alias.
return
ChildProcess|null
Returns the restarted ChildProcess instance, or null if the process could not be restarted.
use Native\Desktop\Facades\ChildProcess;

$process = ChildProcess::restart('vite');

message()

Send a message to a running child process.
message
string
required
The message to send to the process.
alias
string|null
The alias of the process to send the message to. If called on an instance, defaults to that instance’s alias.
return
ChildProcess
Returns the ChildProcess instance for method chaining.
use Native\Desktop\Facades\ChildProcess;

ChildProcess::message('reload', 'vite');

Properties

The ChildProcess class exposes the following readonly properties:
pid
int
The process ID of the child process.
alias
string
The unique identifier for this process.
cmd
array
The command that was executed, as an array.
cwd
string|null
The working directory of the process.
env
array|null
The environment variables passed to the process.
persistent
bool
Whether the process is set to restart automatically.
iniSettings
array|null
PHP ini settings applied to the process (PHP processes only).
use Native\Desktop\Facades\ChildProcess;

$process = ChildProcess::get('vite');

echo "PID: {$process->pid}\n";
echo "Alias: {$process->alias}\n";
echo "Command: " . implode(' ', $process->cmd) . "\n";
echo "Persistent: " . ($process->persistent ? 'Yes' : 'No') . "\n";

Build docs developers (and LLMs) love