Plesk

Manage Services

Note: Only Linux services can be managed with the help of this SDK API.

Plesk provides a basic functionality for services management. This SDK
API allows extensions to register their own services such that they are
transparent for the end-user.

Services in SDK are introduced by the
pm_SystemService_Service
abstract class. Each registered service should be derived from it, and
should implement the
getName()
method that defines the service display name. The
getId()
method defines the service identifier. It is obtained from the service
class name by excluding the prefix & the module name (for example,
Modules_TrafficMonitor_Monitor_Service => monitor_service,
PleskExtTrafficMonitorMonitorService => monitorservice,
MonitorService => monitorservice). Feel free to redefine it.

Service management is performed via the
onStart(),
onStop(),
and
onRestart()
methods. A service’s state can be obtained via the
isInstalled(),
isConfigured(),
and
isRunning()
methods that return “true” by default.

Each service must be registered in the hook. The class
Modules_<Extension name>_SystemServices must to be derived from
pm_Hook_SystemServices
and placed in the plib/hooks/ directory.

To manage services via API-RPC or CLI, use the extended service
identifier: ext-<extension id>-<service id>. For example, in case of
Modules_TrafficMonitor_Service it would be
ext-traffic-monitor-service.

Examples

Example of a service implementation
(PRODUCT_ROOT_D . /admin/plib/modules/traffic-monitor/library/Service.php):

<?php
// Copyright 1999-2016. Parallels IP Holdings GmbH. All Rights Reserved.
/**
 * Service provided by module
 *
 * @package Plesk_Modules
 */
class Modules_TrafficMonitor_Service extends pm_SystemService_Service
{
    public function getName()
    {
        return 'Traffic monitor';
    }
    public function onStart()
    {
        Modules_TrafficMonitor_Agent::enableMonitor();
    }
    public function onStop()
    {
        Modules_TrafficMonitor_Agent::execute(['-stop']);
    }
    public function onRestart()
    {
        Modules_TrafficMonitor_Agent::execute(['-reset']);
    }
    public function isRunning()
    {
        return Modules_TrafficMonitor_Agent::isMonitorEnabled();
    }
}

An example of service registry
(PRODUCT_ROOT_D . /admin/plib/modules/traffic-monitor/hooks/SystemServices.php):

class Modules_TrafficMonitor_SystemServices extends pm_Hook_SystemServices
{
    public function getServices()
    {
        return [new Modules_TrafficMonitor_Service()];
    }
}