IQRF PHP service manager

SystemdManager
in package
implements IServiceManager

Tool for managing services (systemd init daemon)

Service state is read from unit properties (systemctl show), which are machine-readable and do not depend on the systemd version or locale.

Table of Contents

Interfaces

IServiceManager
Interface for tools for managing service

Constants

STATUS_EXIT_CODE_NO_SUCH_UNIT  = 4
Exit code of systemctl status for nonexistent unit

Properties

$commandManager  : CommandExecutor

Methods

__construct()  : mixed
Constructor
disable()  : void
Disables service(s)
enable()  : void
Enables service(s)
exists()  : bool
Checks if the service exists
getState()  : ServiceState
Returns state of the service
getStatus()  : string
Returns status of the service
isActive()  : bool
Checks if the service is active
isEnabled()  : bool
Checks if the service is enabled
reload()  : void
Reloads the service configuration without interrupting the service
restart()  : void
Restarts the service, the service is started if it is not running
start()  : void
Starts service(s)
stop()  : void
Stops service(s)
changeEnablement()  : void
Enables or disables service(s)
checkOperationResult()  : void
Checks the result of the operation with service(s)
formatServiceName()  : string
Formats the service name
formatServiceNames()  : string
Formats service names
getExistingProperties()  : array{LoadState: string, UnitFileState: string, ActiveState: string}
Returns properties of the existing unit
getProperties()  : array{LoadState: string, UnitFileState: string, ActiveState: string}
Returns unit properties
getUnitFileState()  : SystemdUnitFileState|null
Returns the unit file state from unit properties
isUnit()  : bool
Checks if service name already contains a unit type
runOperation()  : void
Runs the operation with service(s)

Constants

STATUS_EXIT_CODE_NO_SUCH_UNIT

Exit code of systemctl status for nonexistent unit

private mixed STATUS_EXIT_CODE_NO_SUCH_UNIT = 4

Properties

$commandManager read-only

private CommandExecutor $commandManager

Methods

__construct()

Constructor

public __construct(CommandExecutor $commandManager) : mixed
Parameters
$commandManager : CommandExecutor

Command manager

disable()

Disables service(s)

public disable(string|array<string|int, string> $services[, bool $stop = true ]) : void
Parameters
$services : string|array<string|int, string>

Service name(s)

$stop : bool = true

Stop service(s) after disabling

Tags
throws
NonexistentServiceException

The service does not exist

throws
ServiceOperationException

The service cannot be disabled (e.g. static unit) or the operation failed

enable()

Enables service(s)

public enable(string|array<string|int, string> $services[, bool $start = true ]) : void
Parameters
$services : string|array<string|int, string>

Service name(s)

$start : bool = true

Start service(s) after enabling

Tags
throws
NonexistentServiceException

The service does not exist

throws
ServiceOperationException

The service cannot be enabled (e.g. static unit) or the operation failed

exists()

Checks if the service exists

public exists(string $serviceName) : bool
Parameters
$serviceName : string

Service name

Return values
bool —

Does the service exist?

getState()

Returns state of the service

public getState(string $serviceName[, bool $withStatus = false ]) : ServiceState
Parameters
$serviceName : string

Service name

$withStatus : bool = false

Include service status?

Tags
throws
NonexistentServiceException

The service does not exist

Return values
ServiceState —

Service state

getStatus()

Returns status of the service

public getStatus(string $serviceName) : string
Parameters
$serviceName : string

Service name

Tags
throws
NonexistentServiceException

The service does not exist

Return values
string —

Service status

isActive()

Checks if the service is active

public isActive(string $serviceName) : bool
Parameters
$serviceName : string

Service name

Tags
throws
NonexistentServiceException

The service does not exist

Return values
bool —

Is service active?

isEnabled()

Checks if the service is enabled

public isEnabled(string $serviceName) : bool
Parameters
$serviceName : string

Service name

Tags
throws
NonexistentServiceException

The service does not exist

Return values
bool —

Is service enabled?

reload()

Reloads the service configuration without interrupting the service

public reload(string $serviceName) : void
Parameters
$serviceName : string

Service name

Tags
throws
NonexistentServiceException

The service does not exist

throws
ServiceOperationException

The operation failed (e.g. the service does not support reloading or is not running)

changeEnablement()

Enables or disables service(s)

private changeEnablement("enable"|"disable" $operation, string|array<string|int, string> $services, bool $now) : void
Parameters
$operation : "enable"|"disable"

Operation

$services : string|array<string|int, string>

Service name(s)

$now : bool

Start or stop service(s) immediately

Tags
throws
NonexistentServiceException

The service does not exist

throws
ServiceOperationException

The service cannot be enabled or disabled (e.g. static unit) or the operation failed

checkOperationResult()

Checks the result of the operation with service(s)

private checkOperationResult(ICommand $command, array<string|int, string> $services) : void
Parameters
$command : ICommand

Executed command

$services : array<string|int, string>

Service names

Tags
throws
NonexistentServiceException

The service does not exist

throws
ServiceOperationException

The operation failed

formatServiceName()

Formats the service name

private formatServiceName(string $serviceName) : string
Parameters
$serviceName : string

Service name to format

Return values
string —

Formatted service name

formatServiceNames()

Formats service names

private formatServiceNames(array<string|int, string> $services) : string
Parameters
$services : array<string|int, string>

Service names

Return values
string —

Formatted service names

getExistingProperties()

Returns properties of the existing unit

private getExistingProperties(string $serviceName) : array{LoadState: string, UnitFileState: string, ActiveState: string}
Parameters
$serviceName : string

Service name

Tags
throws
NonexistentServiceException

The service does not exist

Return values
array{LoadState: string, UnitFileState: string, ActiveState: string} —

Unit properties

getProperties()

Returns unit properties

private getProperties(string $serviceName) : array{LoadState: string, UnitFileState: string, ActiveState: string}
Parameters
$serviceName : string

Service name

Return values
array{LoadState: string, UnitFileState: string, ActiveState: string} —

Unit properties

getUnitFileState()

Returns the unit file state from unit properties

private getUnitFileState(array<string, string> $properties) : SystemdUnitFileState|null
Parameters
$properties : array<string, string>

Unit properties

Return values
SystemdUnitFileState|null —

Unit file state, null if unknown

isUnit()

Checks if service name already contains a unit type

private isUnit(string $serviceName) : bool
Parameters
$serviceName : string

Service name

Return values
bool —

Service name contains unit type

runOperation()

Runs the operation with service(s)

private runOperation(string $operation, string|array<string|int, string> $services) : void
Parameters
$operation : string

Operation (systemctl command)

$services : string|array<string|int, string>

Service name(s)

Tags
throws
NonexistentServiceException

The service does not exist

throws
ServiceOperationException

The operation failed


        
On this page

Search results