April 14, 2010

Running a Background Process from PHP on Linux

While PHP is capable of handling just about anything, there are times when you must take the load away from PHP and hand it over to another program. Processing huge amount of data is one case; examples include sending thousands of emails, moving thousands of files from one location to another, batch processing megabytes of image data or converting an uploaded video from one format to another.

Scenarios like these pose a few challenges. You cannot expect the user to wait for too long. You cannot handover the program control to a time consuming script. And you will have to deal with time limits both on the server and client sides.

A solution to these problems is to spawn a background process and transfer the processing load to it. A background process returns control immediately to the calling program and continues.