PHP Classes

How to Implement a PHP Long Running Process that Uses Linux Systemd to Monitor the Process State - PHP Systemd Daemon Notify package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP Systemd Daemon Notify PHP Systemd Daemon Notify   Blog PHP Systemd Daemon Notify package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement a PH...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Viewers: 577

Last month viewers: 2

Package: PHP Systemd Daemon Notify

Systemd is a set of software blocks that makes part of current Linux distributions to help building tools that perform tasks that can take a long time to finish, like for instance sending newsletters to many subscribers.

Read this article to learn how you can take advantage of the systemd software to simplify the implementation of your own long running PHP processes.




Loaded Article

In this article you will learn:

What is Linux Systemd?

How to Take Advantage of Linux Systemd in Your PHP Applications

How to Download PHP Systemd Daemon Notify Package or Install it with PHP Composer


What is Linux Systemd?

Systemd is a set of software blocks that makes part of current Linux distributions to help building tools that perform tasks that can take a long time to finish, like for instance sending newsletters to many subscribers.

Systemd has been around for more than 11 years now and is now quite ubiquitous in the Linux world.

The main task of Systemd is to start, stop, and monitor programs that implement services.

Service programs are described in simple configuration files. These files are highly portable between different Linux distributions.

Services can talk back to systemd through a notification socket connection. Using this connection, they can give a brief status description of the service, tell systemd what is the main process identifier number (PID), among other details.

Of particular interest are the notifications of type "READY" and "WATCHDOG"

The "READY" type notification tells systemd that the service has successfully started and is up and running.

The "WATCHDOG" type notification are called heartbeats and can tell systemd that the service is still alive and working correctly.

In combination with the service description in the configuration file and the WATCHDOG heartbeats, systemd can detect when the service needs to be restarted.

The benefit of using this system is that you have much more fine-grained control on how quickly your service is restarted during an error condition, as well that the service only is restarted when there is an actual problem.

We can also provide a status message to Systemd which will be displayed when you execute the following command, on which service is the name of the service process that is enquired to get its status.

systemctl status service

How to Take Advantage of Linux Systemd in Your PHP Applications

The PHP Systemd Daemon Notify package can be used to help you taking advantage of the Linux Systemd in your PHP applications.

To show you how can you use this in practice, I have provided an example service to show this in action. You can find the example code in Github or here in the PHP Classes site.

Installing the Example

After downloading the example code to /opt/systemd-notify/examples/lookupd directory and switching to it, all we have to do is composer install followed by sudo install lookupd.service

Running and Testing the Daemon

Starting the daemon now is as easy as just asking systemd to start it using the command:

sudo systemctl start lookupd

If the service started correctly, no further output is produced.

Let's ask systemd how our service is doing:

[Andy@Awesome lookupd]$ sudo systemctl status lookupd
lookupd.service - Example service to lookup hostnames
   Loaded: loaded (/etc/systemd/system/lookupd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2017-10-29 15:00:01 GMT; 5s ago
 Main PID: 32033 (php)
   Status: "Listening on 127.0.0.1:7777; Served 0 client(s)"
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/lookupd.service
           └─32033 /usr/bin/php /opt/systemd-notify/examples/lookupd/lookupd-start.php

This output tells us several things. It shows that the service is running properly. It is sending heartbeat (watchdog) events. It shows a status message.

A script has also been provided to test the daemon. Let's run that a couple of times:

[Andy@Awesome lookupd]$ for counter in {1..10}; do php test-lookup.php; done
localhost.localdomain
localhost.localdomain
localhost.localdomain
localhost.localdomain
localhost.localdomain
localhost.localdomain
localhost.localdomain
localhost.localdomain
localhost.localdomain
localhost.localdomain

Now check the status again:

[Andy@Awesome lookupd]$ sudo systemctl status lookupd
lookupd.service - Example service to lookup hostnames
   Loaded: loaded (/etc/systemd/system/lookupd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2017-10-29 15:09:05 GMT; 9min ago
 Main PID: 32033 (php)
   Status: "Listening on 127.0.0.1:7777; Served 10 client(s)"
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/lookupd.service
           └─32033 /usr/bin/php /opt/systemd-notify/examples/lookupd/lookupd-start.php

Of course this is a contrived example but it clearly demonstrates how this package can be used in your own services.

How to Download PHP Systemd Daemon Notify Package or Install it with PHP Composer

You can use this package by downloading its code from the download page or install it using the PHP Composer tool following the instructions in the package install page.

If you liked comment on this package and the article, please post a comment below, so I can know more how you liked and hopefully I can improve it further.




You need to be a registered user or login to post a comment

1,611,081 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

No comments were submitted yet.



  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   PHP Systemd Daemon Notify PHP Systemd Daemon Notify   Blog PHP Systemd Daemon Notify package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement a PH...