Transform any class in a *nix daemon process or cron job without changes or refactoring.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
scripts
services
src
template
.gitignore
.travis.yml
LICENSE
README.md
composer.json
example.php

README.md

PHP Daemonize

SensioLabsInsight Code Climate Scrutinizer Code Quality

Description

Transform any class in a *nix daemon process or cron job without changes or refactoring.

Motivation

Some times we need to create a cron tab or a process for running in background. The most of times we need to create a new class, probably in a different framework and have to set or even choose another language for create the job/daemon.

"Daemonize" enables you to can create a linux daemon or a job for use in a cron tab without change you pre-existing class.

"Daemonize" is a script that create a "init.d" script and encapsulate or class enabling you to run it in the bash, for example.

How to

Suppose you have a pre-existing class for read some info from database and run some action with these data. For example:

namespace Some\Name\Space;

class MyExistingClass
{
	// ...

    public function someExistingMethod()
    {
        // Your code
    }

	// ...
}

If you want transform this class and method in a linux daemon (or "daemonize" it) you have to first create a bootstrap php file. This file will tell to the script all setup you need to run this class.

The most simple bootstrap.php is:

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

Now, if you want to test it you can run the command:

daemonize run "\\Some\\Name\\Space\\MyExistingClass::someExistingMethod" "relative/path/to/bootstrap.php" "/path/to/root"

If everything is ok, now you can "daemonize" this class (as root):

daemonize install --template=upstart mydaemon "\\Some\\Name\\Space\\MyExistingClass::someExistingMethod" "relative/path/to/bootstrap.php" "/path/to/root"

note: valid templates are: upstart or initd (default)

Now for start or stop the service you need only

sudo service mydaemon start  # or stop, status or restart

For uninstall just type:

daemonize uninstall mydamon

and list all "daemonized" php classes

daemonize services --only-names

Install

Daemonize does not need to be associated to your PHP project. Prefer install as a global package and as root user.

composer global require "byjg/php-daemonize=1.3.*"
sudo ln -s /root/.composer/vendor/bin/daemonize /usr/local/bin/daemonize

If you want to share this installation with another users consider use the command chmod a+x /root. The root directory will remain unreadable for them, but you'll can execute the script "daemonize".

Running a pre-installed demo

Open two terminals.

First do :

touch /etc/tryme.txt
tail -f /etc/tryme.txt

On the second do:

sudo daemonize install --template=upstart tryme "\\ByJG\\Daemon\\Sample\\TryMe::process" "vendor/autoload.php" "./"

sudo service tryme start

If everything is OK, will see on the first terminal a lot of lines added. Do not forget to run sudo service tryme stop