You are here
PHP daemons
A daemon (data and execution monitor) is a piece of software, script or native program that usually runs in the background, without direct communication with the user (i.e., no console/whatever exchange per se).
Although writing daemons in PHP isn't the best idea (larger overhead, relatively high memory consumption), it can be convenient. I attach the sample PHP daemon (stripped down version of real-life PHP daemon I use for real-life sites). This piece of software is released under Lesser GNU Public License version 3 (included in the distribution archive). Generally it means you can derive commercial version of it without providing sources of them to general public. However, I suggest providing any derived work LGPL-, or compatibly licensed.
Download the script here: php-daemon.zip.
Short description follows.
Prerequisites:
Script consists of executable (PHP CLI required) and an INI file. The INI file name is constructed by replacing .php part of the script name with .ini or attaching .ini, if the above results in nothing. The following INI directives are recognized by the script in its current version:
- self_name
- (string) Script name. Set to any string you like.
- self_version
- (string) Script version. Set to any string you like.
- dsn
- (DSN-syntax string, e.g. "mysql://dbuser:dbpass@localhost/dbname"). Set to any valid DSN. Worker will attempt to connect to it, unless you remove that capability.
- include
- (string) additional include paths, in suitable for PHP syntax (as for include_dir)
- max_workers
- (integer) Number of concurrent worker processes. Set to a reasonable value.
- worker_poll
- (integer) The master (parent) process will awake every that many seconds and if worker processes counter is below 'max_workers', will launch another worker.
- port
- (integer) Communication port to interact with the daemon.
- log_format
- (date-compatible format) This string is passed to PHP date() function to form a date/time prefix used when writing to log file.
- log_file
- (sprintf string) A log file template string, passed to sprintf(), with the the results of 'log_format' processed passed. Should contain the only '%s' format macro.
The script can be run in two modes:
./php-daemon.php daemon Launch in daemon modeor
./php-daemon.php worker Launch a single worker processThe latter is useful to debug worker code.
When the daemon starts in daemon mode, it
- goes into daemon mode, closing all standard input/output streams)
- opens and listens to the specified port, waiting for commands
- every 'worker_poll' seconds checks for a number of alive worker processes, and if that is less than 'max_workers', launches another worker process
- when the above port is opened via Telnet, expects 'info' or 'quit' commands (the latter shuts down the script and every worker process still active by sending signal 15, SIGTERM)
Put your useful worker code into into worker_process() function and, of course, feel free to add more type of child processes to your taste.
To see the output, use 'write_log()' function to put messages into the daemon log. Otherwise, you have no means of communicating with it.
The script contains basic DB-processing functions suitable for MysQL/MariaDB/compatible cases. remove them if not required.
Stay tuned for possible real-life samples of the daemon script and send links to your implementation of useful tools based on it. Good luck!