|
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
|
|
|
|
|
18
|
|
|
class Service |
|
19
|
|
|
{ |
|
20
|
|
|
const STATUS_STOPPED = 3; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $command = 'systemctl'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var bool |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $sudo = true; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
private static $timeout = 3; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $name; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
65 |
|
* Sets the systemctl command to use. |
|
44
|
|
|
* |
|
45
|
65 |
|
* @param string $command |
|
46
|
65 |
|
*/ |
|
47
|
|
|
public static function setCommand($command) |
|
48
|
|
|
{ |
|
49
|
|
|
self::$command = $command; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
65 |
|
* Specifies whether or not to use sudo to run the systemctl command. |
|
54
|
|
|
* |
|
55
|
65 |
|
* @param bool $flag |
|
56
|
65 |
|
*/ |
|
57
|
|
|
public static function sudo($flag = true) |
|
58
|
|
|
{ |
|
59
|
|
|
self::$sudo = (bool) $flag; |
|
60
|
|
|
} |
|
61
|
65 |
|
|
|
62
|
|
|
/** |
|
63
|
65 |
|
* Specifies the timeout in seconds for the systemctl process. |
|
64
|
65 |
|
* |
|
65
|
|
|
* @param int $timeout |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function setTimeout($timeout) |
|
68
|
|
|
{ |
|
69
|
|
|
self::$timeout = (int) $timeout; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
45 |
|
* @param string $name Name of the service to manage |
|
74
|
|
|
*/ |
|
75
|
45 |
|
public function __construct($name) |
|
76
|
45 |
|
{ |
|
77
|
|
|
$this->name = $name; |
|
78
|
45 |
|
} |
|
79
|
|
|
|
|
80
|
45 |
|
/** |
|
81
|
|
|
* Checks whether or not the service is running. |
|
82
|
45 |
|
* |
|
83
|
20 |
|
* @throws CommandFailedException If the command failed |
|
84
|
|
|
* |
|
85
|
25 |
|
* @return bool |
|
86
|
20 |
|
*/ |
|
87
|
|
|
public function isRunning() |
|
88
|
|
|
{ |
|
89
|
5 |
|
$process = $this->getProcess([ |
|
90
|
|
|
'--lines=0', |
|
91
|
|
|
'status', |
|
92
|
|
|
$this->name, |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
|
|
$process->run(); |
|
96
|
|
|
|
|
97
|
15 |
|
if ($process->isSuccessful()) { |
|
98
|
|
|
return true; |
|
99
|
15 |
|
} |
|
100
|
5 |
|
if (self::STATUS_STOPPED === $process->getExitCode()) { |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
10 |
|
|
|
104
|
10 |
|
throw new CommandFailedException($process); |
|
105
|
|
|
} |
|
106
|
10 |
|
|
|
107
|
|
|
/** |
|
108
|
10 |
|
* Starts the service. |
|
109
|
|
|
* |
|
110
|
10 |
|
* @throws CommandFailedException If the command failed |
|
111
|
5 |
|
*/ |
|
112
|
|
|
public function start() |
|
113
|
5 |
|
{ |
|
114
|
|
|
if ($this->isRunning()) { |
|
115
|
|
|
return; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$process = $this->getProcess([ |
|
119
|
|
|
'start', |
|
120
|
15 |
|
$this->name, |
|
121
|
|
|
]); |
|
122
|
15 |
|
|
|
123
|
5 |
|
$process->run(); |
|
124
|
|
|
|
|
125
|
|
|
if (!$process->isSuccessful()) { |
|
126
|
10 |
|
throw new CommandFailedException($process); |
|
127
|
10 |
|
} |
|
128
|
|
|
} |
|
129
|
10 |
|
|
|
130
|
|
|
/** |
|
131
|
10 |
|
* Stops the service. |
|
132
|
|
|
* |
|
133
|
10 |
|
* @throws CommandFailedException If the command failed |
|
134
|
5 |
|
*/ |
|
135
|
|
|
public function stop() |
|
136
|
5 |
|
{ |
|
137
|
|
|
if (!$this->isRunning()) { |
|
138
|
|
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$process = $this->getProcess([ |
|
142
|
|
|
'stop', |
|
143
|
15 |
|
$this->name, |
|
144
|
|
|
]); |
|
145
|
15 |
|
|
|
146
|
15 |
|
$process->run(); |
|
147
|
|
|
|
|
148
|
15 |
|
if (!$process->isSuccessful()) { |
|
149
|
|
|
throw new CommandFailedException($process); |
|
150
|
15 |
|
} |
|
151
|
|
|
} |
|
152
|
15 |
|
|
|
153
|
10 |
|
/** |
|
154
|
|
|
* Restarts the service. |
|
155
|
5 |
|
* |
|
156
|
|
|
* @throws CommandFailedException If the command failed |
|
157
|
|
|
*/ |
|
158
|
|
|
public function restart() |
|
159
|
|
|
{ |
|
160
|
5 |
|
$process = $this->getProcess([ |
|
161
|
|
|
'restart', |
|
162
|
5 |
|
$this->name, |
|
163
|
|
|
]); |
|
164
|
|
|
|
|
165
|
|
|
$process->run(); |
|
166
|
|
|
|
|
167
|
|
|
if (!$process->isSuccessful()) { |
|
168
|
|
|
throw new CommandFailedException($process); |
|
169
|
|
|
} |
|
170
|
60 |
|
} |
|
171
|
|
|
|
|
172
|
60 |
|
/** |
|
173
|
60 |
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
|
|
public function __toString() |
|
176
|
|
|
{ |
|
177
|
60 |
|
return $this->name; |
|
178
|
60 |
|
} |
|
179
|
|
|
|
|
180
|
60 |
|
/** |
|
181
|
|
|
* Creates and prepares a process. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string[] $arguments |
|
184
|
|
|
* |
|
185
|
|
|
* @return Process |
|
186
|
|
|
*/ |
|
187
|
|
|
private function getProcess(array $arguments) |
|
188
|
|
|
{ |
|
189
|
|
|
$command = explode(' ', self::$command); |
|
190
|
|
|
if (self::$sudo) { |
|
191
|
|
|
array_unshift($command, 'sudo'); |
|
192
|
|
|
} |
|
193
|
|
|
$command = array_merge($command, $arguments); |
|
194
|
|
|
|
|
195
|
|
|
$process = new Process($command); |
|
196
|
|
|
$process->setTimeout(self::$timeout); |
|
197
|
|
|
|
|
198
|
|
|
return $process; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|