PHP Classes

PHP Systemd Daemon Notify: Interact with Systemd Notification Deamon

Recommend this page to a friend!
  Info   View files Documentation   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 215 This week: 1All time: 8,313 This week: 571Up
Version License PHP version Categories
systemd-notify 1.0.1GNU General Publi...7.1Unix, Systems administration, PHP 7
Description 

Author

This package allows PHP applications to interact with the Systemd Notification daemon.

It sends feedback to the Systemd daemon about the runtime status
of a service.

A service periodically sends watchdog (heartbeat) events to
Systemd's notification daemon. When the watchdog (heartbeat) events
stop, Systemd knows the service has a problem and can restart it.

This package provides classes to interact with the notification service by sending heartbeats.

Innovation Award
PHP Programming Innovation award winner
October 2017
Winner


Prize: One big elePHPant Plush Mascott
Systemd Notification daemon.

It sends feedback to the Systemd daemon about the runtime status
of a service.

A service periodically sends watchdog (heartbeat) events to
Systemd's notification daemon. When the watchdog (heartbeat) events
stop, Systemd knows the service has a problem and can restart it.

This package provides classes to interact with the notification service by sending heartbeats.

Manuel Lemos
Picture of Andy Pieters
  Performance   Level  
Name: Andy Pieters <contact>
Classes: 1 package by
Country: United Kingdom
Age: 46
All time rank: 4093180 in United Kingdom
Week rank: 420 Up10 in United Kingdom Up
Innovation award
Innovation award
Nominee: 1x

Winner: 1x

Documentation

systemd-notify example daemon

Installing the example

After downloading the example to the /opt/systemd-notify/examples/lookupd directory, cd to the directory and execute:

composer install

Edit the lookupd.service file and adjust the User, and Group entries according to your needs, then execute:

sudo install lookupd.service

Running & testing the daemon

Starting the daemon is now as easy as just asking systemd: sudo systemctl start lookupd and if the service started correctly, no further output is produced.

Getting the service status

We can now query the status of our service by asking systemd:

[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 and sending heartbeat (watchdog) events, and 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.


Details

systemd-notify

Introduction

This package allows scripts launched by systemd to send information and heartbeats (watchdog events) to the systemd notification system.

Use this library if...

You have a script that is started as a service by systemd, and you want systemd to restart the service when it develops an error, or

You have a long running script and want to provide a status description viewable by the systemctl status myservice command.

Do not use this library if...

  • The system is not run by SystemD (e.g. FreeBSD, Windows, ...)
  • Your service is not going to be started by Systemd

In this library

Three classes are provided to interact with the notification service:

  1. Notify is the general purpose implementation.
  2. NotifyHeartbeat is ideal for applications that only send heartbeats
  3. NotifyFluent has the same methods as Notify but all the setters are fluent.

Examples

See the examples folder


  Files folder image Files  
File Role Description
Files folder imageexamples (1 directory)
Files folder imagesrc (6 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  examples  
File Role Description
Files folder imagelookupd (6 files)

  Files folder image Files  /  examples  /  lookupd  
File Role Description
  Accessible without login Plain text file composer.json Data Auxiliary data
  Accessible without login Plain text file lookupd-start.php Aux. Auxiliary script
  Plain text file lookupd.php Class Class source
  Accessible without login Plain text file lookupd.service Data Auxiliary data
  Accessible without login Plain text file README.md Doc. Documentation
  Accessible without login Plain text file test-lookup.php Aux. Auxiliary script

  Files folder image Files  /  src  
File Role Description
  Plain text file NotifierError.php Class Class source
  Plain text file Notify.php Class Class source
  Plain text file NotifyBase.php Class Class source
  Plain text file NotifyFluent.php Class Class source
  Plain text file NotifyHeartbeat.php Class Class source
  Plain text file SystemdNotifierInterface.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:215
This week:1
All time:8,313
This week:571Up