• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Running background processes in PHP

By Akash Mehta | on May 18, 2008 | 3 Comments
PHP Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

So you’ve just built a fantastic processing routine for your application. You’ve checked and double checked the integrity of user input, and you’re doing some serious processing. There’s only one problem: it’s too slow. There’s a simple solution: forking your processing script, and running the code as a background process asynchronously. It can email your user when it’s done: they’ll wait. In this tutorial, I’ll show you how to get started with background processes in PHP.

When we want some kind of background process running our PHP code, we have a simple challenge: we can’t leave it up to the client. On the web, people hit their stop button, their browsers lock up and they force quit, they even have the odd power outage. When we want to do complex processing on the server side, we need to detach from the client-server model of a web-served script. PHP can fork a script, but the parent script is still facing an unreliable client.

Our best option, then, is to launch a background process from the command line. There are three steps to this: starting the background script, passing it information to do its job, and checking when it’s finished.

Starting the background script bolt dvd download
On UNIX systems, we can easily call another PHP script via the shell, set it to run off in the background and send its output to /dev/null. Here’s a sample:

exec ("/usr/bin/php proc.php >/dev/null &");

Here, we call the exec function; instructing it to run our local PHP binary – usually at /usr/bin/php or /usr/local/bin/php, check with your host/sysadmin. We pass it a single argument: the filename of the PHP script that handles our background process. We then send the output to /dev/null, effectively destroying it. The last character, the & symbol, is important – this tells the system to start this process in the background and to let us continue.

Talking to the background process
It’s all very well to start a background process, but now it needs to know what to do. We handle this through command line arguments. Let’s update the previous example:

exec ("/usr/bin/php proc.php --task=50 >/dev/null &");

Our proc.php file might look like this:


Running "php proc.php --task=50" on command line produces the following output:

Handling process --task=50

From here, you can easily design your script to do what it needs to.

Monitoring the background process
There's just one last detail: how do we monitor the progress of our background process? It's best to use an independent datastore that can be relied upon, instead of communicating with running threads - your application's MySQL database would be perfect. You can create a "sessions" or "jobs" table with an ID column, a status column (started, in progress, finished - 0, 1, 2), and any other information you need. Before you start your background process, create a new entry in this table and check the insert ID, then pass it to the background process when you start it:

exec ("/usr/bin/php proc.php --some_info=50 --job=1234 >/dev/null &");

Your proc.php file can then update the table row with the ID you supply it, and your front end applications can provide status updates to end users by checking up on all pending jobs for the current user - relatively straightforward business logic.

If you're going to be running many background processes, a job queue and an "always-on" script running on your server may also be an option. In this case, consider looking into pcntl_fork() and the program execution functions.

Share this story:
  • tweet

Author Description

3 Responses to “Running background processes in PHP”

  1. July 18, 2008

    David Dashifen Kees Log in to Reply

    This reminds me a little of using AJAX to send the appropriate inputs to the server for complex calculations while the client continues to go about their business. When the server’s done, it can save the data in the session or in the database and the client can check back later on when appropriate to get the results.

  2. July 18, 2008

    Tiago Log in to Reply

    All servers (by defalt) enable this kind of shell command ?

  3. November 16, 2012

    Shaked Klein Orbach Log in to Reply

    Can you give an example?

    As see it, you can use this only when you are not expecting a response back don’t need to make sure that the command worked as expected, so by saying “updating a table” what if something went wrong and the table was not updated?

    Thank you

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 1,242Followers 492Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.