Execute PHP command in background with Pimcore
What you'll learn with this guide
This guide will show you how to trigger a command that will create a new process separate from the one that’s currently running. Useful when there is a need for running time-consuming operations.
Where to use
To put it in use, let's say you want to process a file which the user has uploaded to the server. If this process can take some time, it's good practice not to keep the user waiting. Instead, you can start a background process that won't be affected by user actions.
In a flow chart it would look something like this:
Solution with background process mentioned in the example above can be done by running:
How it works
Method for running PHP script in the background works on Unix and Windows operating systems, and the above-described statement is in the end executed as:
With the command above, using “nohup” and “nice”, we start a process in the background with priority set to 19 (lowest priority, -20 is the highest), and all of it will be logged to “/output/path” (default is “/dev/null” which will discard output result).
if we run the above command in the terminal and close it, the process would still be running until the script is finished.
The same as on Unix, the command above carries out the given PHP command using the shell, which is started with the PHP COM module and then terminates when it’s finished.
Does it work?
If the command had some output or an exception while running it, it would be logged in the output file, provided in a method call for the background process.