Running a PHP script in the background

Posted in PHP
January 13, 2009

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.

  • Richard

    Just hope you have a web server that
    a) allows you to fork or whatever to allow things to run in the background and
    b) doesn’t kill scripts that take too long to run in the background.
    c) be able to monitor the results of the scripts so you know what works and what failed.

    Given how restrictive most web hosting companies are you need to develop the plugin so that it can run at any cheap resource-Nazi shared hosting provider.

  • http://iarematt.com Matt

    There are a few ways to make sure the script doesn’t time out. On PHP.net a couple people have shared their scripts for doing so: http://lu.php.net/manual/en/function.set-time-limit.php

    I am currently building in logging to make sure I know exactly where in the process the script currently is. I will then use AJAX to display a notice in the WordPress admin area. It will also log exactly which plugins upgraded and which didnt :)

  • http://www.xboxgaming.co.za/ Steven

    I had the problem on hetzner servers that I couldn’t flush any data. Something to do with their implementation of php-cgi. So running something client side (through a browser) wouldn’t work – it ran longer than 10min. Anyways, the only alternative I found was opening a url with fsockopen, and in the script being called, use the ignore_user_abort function. Works like a charm, just make sure it doesn’t hit the memory limit. Otherwise it the process gets killed and you’re sitting wondering what happened.

  • http://iarematt.com Matt

    Hey Steven,

    Thanks for the advice. I am definitely leaning towards the fsockopen method. It seems the more reliable method.