Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

PHP Daemonize

SensioLabsInsight Code Climate Scrutinizer Code Quality

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


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


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


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




No packages published
You can’t perform that action at this time.