Execute a background task in PHP

There are a ton of ways of running a task in the background (like a daemon, etc) from PHP but this must surely be the slickest –

sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile)

This will execute $cmd, writing it’s output to $outputfile, and saving it’s pid in $pidfile. The last two can be /dev/null of course.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s