Daemon in PHP

posted 31.08.2013

PHP is my first choice scripting language. I know it bottom up and everything from file manipulation to network connections can be done both easily and effectively. In the past year or two, the PHP core dev team managed to remove all memory leaks I encountered during writing of long running scripts. Thanks to that, I could start writing background running task scripts in PHP. I used an implementation in Python to get the basic skeleton working and subclassed my scripts from it. You can do exactly the same!

Usage

composer require vvondra/php-daemon Build Status
<?php

require_once __DIR__ . '/../vendor/autoload.php';

class Sleeper extends PHPDaemon\Daemon {

  public function run() {
		while (true) {
			echo "zzzZZZzzz...\n";
			sleep(2);
		}
	}
}

PHPDaemon\Daemon::parseArgs(new Sleeper);

Save the code above as sleeper.php and then just make the appropriate calls:

php sleeper.php start
php sleeper.php stop
php sleeper.php status
comments powered by Disqus