1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the systemctl PHP library. |
7
|
|
|
* |
8
|
|
|
* (c) Martin Janser <[email protected]> |
9
|
|
|
* |
10
|
|
|
* This source file is subject to the GPL license that is bundled |
11
|
|
|
* with this source code in the file LICENSE. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace SystemCtl; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\Process\Process; |
17
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
18
|
|
|
|
19
|
|
|
class Service |
20
|
|
|
{ |
21
|
|
|
const STATUS_STOPPED = 3; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private static $command = 'systemctl'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private static $sudo = true; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Sets the systemctl command to use. |
40
|
|
|
* |
41
|
|
|
* @param string $command |
42
|
|
|
*/ |
43
|
65 |
|
public static function setCommand($command) |
44
|
|
|
{ |
45
|
65 |
|
self::$command = $command; |
46
|
65 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Specifies whether or not to use sudo to run the systemctl command. |
50
|
|
|
* |
51
|
|
|
* @param bool $flag |
52
|
|
|
*/ |
53
|
65 |
|
public static function sudo($flag = true) |
54
|
|
|
{ |
55
|
65 |
|
self::$sudo = (bool) $flag; |
56
|
65 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $name Name of the service to manage |
60
|
|
|
*/ |
61
|
65 |
|
public function __construct($name) |
62
|
|
|
{ |
63
|
65 |
|
$this->name = $name; |
64
|
65 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checks whether or not the service is running. |
68
|
|
|
* |
69
|
|
|
* @throws CommandFailedException If the command failed |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
45 |
|
public function isRunning() |
74
|
|
|
{ |
75
|
45 |
|
$builder = $this->getProcessBuilder(); |
76
|
45 |
|
$builder->add('--lines=0')->add('status')->add($this->name); |
77
|
|
|
|
78
|
45 |
|
$process = $builder->getProcess(); |
79
|
|
|
|
80
|
45 |
|
$process->run(); |
81
|
|
|
|
82
|
45 |
|
if ($process->isSuccessful()) { |
83
|
20 |
|
return true; |
84
|
|
|
} |
85
|
25 |
|
if (self::STATUS_STOPPED === $process->getExitCode()) { |
86
|
20 |
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
5 |
|
throw new CommandFailedException($process); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Starts the service. |
94
|
|
|
* |
95
|
|
|
* @throws CommandFailedException If the command failed |
96
|
|
|
*/ |
97
|
15 |
|
public function start() |
98
|
|
|
{ |
99
|
15 |
|
if ($this->isRunning()) { |
100
|
5 |
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
10 |
|
$builder = $this->getProcessBuilder(); |
104
|
10 |
|
$builder->add('start')->add($this->name); |
105
|
|
|
|
106
|
10 |
|
$process = $builder->getProcess(); |
107
|
|
|
|
108
|
10 |
|
$process->run(); |
109
|
|
|
|
110
|
10 |
|
if (!$process->isSuccessful()) { |
111
|
5 |
|
throw new CommandFailedException($process); |
112
|
|
|
} |
113
|
5 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Stops the service. |
117
|
|
|
* |
118
|
|
|
* @throws CommandFailedException If the command failed |
119
|
|
|
*/ |
120
|
15 |
|
public function stop() |
121
|
|
|
{ |
122
|
15 |
|
if (!$this->isRunning()) { |
123
|
5 |
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
10 |
|
$builder = $this->getProcessBuilder(); |
127
|
10 |
|
$builder->add('stop')->add($this->name); |
128
|
|
|
|
129
|
10 |
|
$process = $builder->getProcess(); |
130
|
|
|
|
131
|
10 |
|
$process->run(); |
132
|
|
|
|
133
|
10 |
|
if (!$process->isSuccessful()) { |
134
|
5 |
|
throw new CommandFailedException($process); |
135
|
|
|
} |
136
|
5 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Restarts the service. |
140
|
|
|
* |
141
|
|
|
* @throws CommandFailedException If the command failed |
142
|
|
|
*/ |
143
|
15 |
|
public function restart() |
144
|
|
|
{ |
145
|
15 |
|
$builder = $this->getProcessBuilder(); |
146
|
15 |
|
$builder->add('restart')->add($this->name); |
147
|
|
|
|
148
|
15 |
|
$process = $builder->getProcess(); |
149
|
|
|
|
150
|
15 |
|
$process->run(); |
151
|
|
|
|
152
|
15 |
|
if (!$process->isSuccessful()) { |
153
|
10 |
|
throw new CommandFailedException($process); |
154
|
|
|
} |
155
|
5 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
5 |
|
public function __toString() |
161
|
|
|
{ |
162
|
5 |
|
return $this->name; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Creates and prepares a process builder. |
167
|
|
|
* |
168
|
|
|
* @return ProcessBuilder |
169
|
|
|
*/ |
170
|
60 |
|
private function getProcessBuilder() |
171
|
|
|
{ |
172
|
60 |
|
$command = explode(' ', self::$command); |
173
|
60 |
|
if (self::$sudo) { |
174
|
|
|
array_unshift($command, 'sudo'); |
175
|
|
|
} |
176
|
|
|
|
177
|
60 |
|
$builder = ProcessBuilder::create($command); |
178
|
60 |
|
$builder->setTimeout(3); |
179
|
|
|
|
180
|
60 |
|
return $builder; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|