PHP and background processing
Updated on 03 Oct 2020
Run a PHP script in the background
Typical way to run php
on the command line is with the following syntax.
php filename.php
However this will tie up your console window - you wont get access back to the console until the script finishes. In fact you wont be able to close the window down without causing the program execution to crash.
So… how do we run the script in the background (where we get the console window back), and if we’re on a server can we terminate our connection to it, and still run the process in the background? Yes we can and it is called & disown
.
php filename.php & disown
See which PHP scripts are running in the background
If the previous section showed us how to run PHP scripts in the background, then it might be important at some stage to see which scripts are running in the background; and kill
them if necessary.
ps aux | grep php
Killing a background process
If there is ever a need to kill a background process, we do that with the kill
command and pid. The pid is displayed when we used ps
, and we can see and example of killing a process below.