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