start a process in php

To start a new process in PHP, you can use the exec() function. This function allows you to execute a command in the shell and get the output as an array.

Here is an example:

main.php
$command = "ls -la";
$output = array();
exec($command, $output);

print_r($output);
84 chars
6 lines

This will execute the command ls -la in the shell and store the output in the $output variable. The output will be an array with each element representing a line of output from the command.

You can replace the ls -la command with any command that you would like to execute. Note that the exec() function is a blocking function, which means that it will wait for the command to complete before continuing with the rest of the script.

If you want to start a process in the background and not wait for it to complete, you can use the shell_exec() function instead. This function is similar to exec() but does not return the output of the command as an array.

main.php
$command = "nohup php /path/to/script.php > /dev/null 2>&1 &";
shell_exec($command);
85 chars
3 lines

This will start a new PHP process in the background that runs the script located at /path/to/script.php. The nohup command prevents the process from being terminated when the shell is closed. The output of the script is redirected to /dev/null to prevent it from being displayed, and the & symbol at the end of the command runs the process in the background.

related categories

gistlibby LogSnag