Running a PHP script in the background
I am busy developing a WordPress plugin that will automatically update your WordPress plugins in the background without you ever having to click a button. There will be various settings and you can even select plugins that you do not want to update automatically, but anyway, that is besides the point. Because the process of updating all your out-of-date plugins generally takes quite a lot of time, I needed to develop the plugin so that it runs all those update processes in the background. There would be nothing worse than having to wait for all your plugins to update before you can write a post!
So I did my research on running PHP scripts in the background and I was surprised that there was actually quite a lot written about it on the web. It is a fairly simple task and can be done in multiple different ways. The two most common seem to be:
- Use PHP to execute the script via command line in the background using the exec() function
- Using an asynchronous HTTP request. What you do is call the script with an HTTP request and then kill the connection so that you don’t have to wait for everything to process.
I am currently leaning towards the second method but also still exploring the first. If you want a very good tutorial on how to do the above two methods, then check out this tutorial on w-shadow.com. It gives you code samples and explains quite a bit of it.