Daemonize PHP script

If you need to run a PHP script continuously in the background, try pear package System_Daemon.

You can get it using pear install command or download it. Then code your script like following:


#!/usr/bin/php -q
<?php
require_once 'System/Daemon.php';
System_Daemon::setOption('appName', 'my_script_name');
System_Daemon::setOption('authorEmail', 'me@example.com');
System_Daemon::start();
/*
Rest of your code goes here.
*/

// Stop daemon.
if(some_condition())
{
	System_Daemon::stop();
}
?>

Chmod to make it executable and execute it as root/admin user:

chmod 755 /path/to/your/script.php

sudo /path/to/your/script.php

Or get RSS feed

This entry was posted on Saturday, June 6th, 2009 at 4:00 pm and modified on Tuesday, June 9th, 2009 at 7:00 pm. You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.

Comments : 1

  1. Hi, i tried this but it’s failing as it needs the pcntl enabled..how to enable this? any idea?

    pj

Pings/Trackbacks: 3

  1. Daemonize PHP script - WebMaster View | Webmaster Tools
  2. Daemonize PHP script - WebMaster View
  3. XkiD | Daemonize PHP script - WebMaster View | blog.xkid.ro