Skip to main content

PHP Daemonize

Build Status Opensource ByJG GitHub source GitHub license GitHub release

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:

<?php
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.

The most simple bootstrap is vendor/autoload.php but you can create a more complex bootstrap file if you need.

Below is an example of a bootstrap file:

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

// Your code here

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

daemonize run \
"\\Some\\Name\\Space\\MyExistingClass::someExistingMethod" \
--bootstrap "relative/path/to/bootstrap.php" \
--rootdir "/path/to/root" \
--http-get "param1=value1&param2=value2"

You can test with:

daemonize run \
"\\ByJG\\Daemon\\Sample\\TryMe::ping"
--arg value1
--arg value2

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

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

note: valid templates are:

  • systemd (default)
  • upstart
  • initd
  • crond

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. You can either as a global package or as a local package.

composer global require "byjg/php-daemonize"
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

Environment Variables

VariableDescription
DAEMONIZE_MEMORY_LIMITIf set will define the memory_limit for the daemonize process, otherwise won't have a limit.

Dependencies


Open source ByJG