2024-11-10 03:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2024-11-11 01:21:06 +01:00
|
|
|
/**
|
|
|
|
* Namespace for classes and functions related to managing services.
|
|
|
|
* @package NAE\Service
|
|
|
|
*/
|
2024-11-10 03:40:05 +01:00
|
|
|
namespace NAE\Service;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Systemctl Service Management Interface
|
|
|
|
* @author "Marcel Naeve" <php@naeve.info>
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit)
|
|
|
|
*/
|
|
|
|
class Systemctl {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Name of the service to manage
|
|
|
|
*/
|
|
|
|
private $service_name = "";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string $service_name Name of the service to manage
|
|
|
|
*/
|
|
|
|
public function __construct(string $service_name) {
|
|
|
|
$this->service_name = $service_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the Service
|
|
|
|
*
|
|
|
|
* @return bool True if the service was started successfully, false otherwise
|
|
|
|
*/
|
|
|
|
public function start () : bool {
|
|
|
|
exec("systemctl start ".$this->service_name." > /dev/null", $output, $return);
|
|
|
|
return ( $return === 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the Service
|
|
|
|
*
|
|
|
|
* @return bool True if the service was stopped successfully
|
|
|
|
*/
|
|
|
|
public function stop () : bool {
|
|
|
|
exec("systemctl stop ".$this->service_name." > /dev/null", $output, $return);
|
|
|
|
return ( $return === 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restart the Service
|
|
|
|
*
|
|
|
|
* @return bool True if the service was restarted successfully, false otherwise
|
|
|
|
*/
|
|
|
|
public function restart () : bool {
|
|
|
|
exec("systemctl restart ".$this->service_name." > /dev/null", $output, $return);
|
|
|
|
return ( $return === 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the Service is running
|
|
|
|
*
|
|
|
|
* @return bool True if the service is running, false otherwise
|
|
|
|
*/
|
|
|
|
public function isRunning () : bool {
|
|
|
|
|
|
|
|
exec("systemctl status ".$this->service_name, $output, $return);
|
|
|
|
|
|
|
|
$search_string = implode(";", $output);
|
|
|
|
return ( strpos($search_string, "Active: active (running)") >= 0 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|