Sometimes, we want to execute a background process with PHP.
In this article, we’ll look at how to execute a background process with PHP.
How to execute a background process with PHP?
To execute a background process with PHP, we can use the exec
function.
For instance, we write
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
to run echo with %s
as the placeholders.
The arguments that comes after the string are the values for the placeholders.
We call sprintf
to interpolate the placeholders with the values and return the string.
Then we call exec
to run the command string.
Conclusion
To execute a background process with PHP, we can use the exec
function.