1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of cocur/background-process. |
5
|
|
|
* |
6
|
|
|
* (c) 2013-2015 Florian Eckerstorfer |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cocur\BackgroundProcess; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use RuntimeException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* BackgroundProcess. |
16
|
|
|
* |
17
|
|
|
* Runs a process in the background. |
18
|
|
|
* |
19
|
|
|
* @author Florian Eckerstorfer <[email protected]> |
20
|
|
|
* @copyright 2013-2015 Florian Eckerstorfer |
21
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License |
22
|
|
|
* @link https://florian.ec/articles/running-background-processes-in-php/ Running background processes in PHP |
23
|
|
|
*/ |
24
|
|
|
class BackgroundProcess |
25
|
|
|
{ |
26
|
|
|
const OS_WINDOWS = 1; |
27
|
|
|
const OS_NIX = 2; |
28
|
|
|
const OS_OTHER = 3; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $command; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
private $pid; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
protected $serverOS; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $command The command to execute |
|
|
|
|
47
|
|
|
* |
48
|
|
|
* @codeCoverageIgnore |
49
|
|
|
*/ |
50
|
|
|
public function __construct($command = null) |
51
|
|
|
{ |
52
|
|
|
$this->command = $command; |
53
|
|
|
$this->serverOS = $this->getOS(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Runs the command in a background process. |
58
|
|
|
* |
59
|
|
|
* @param string $outputFile File to write the output of the process to; defaults to /dev/null |
60
|
|
|
* currently $outputFile has no effect when used in conjunction with a Windows server |
61
|
|
|
* @param bool $append - set to true if output should be appended to $outputfile |
62
|
|
|
*/ |
63
|
2 |
|
public function run($outputFile = '/dev/null', $append = false) |
64
|
|
|
{ |
65
|
2 |
|
if($this->command === null) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
switch ($this->getOS()) { |
70
|
2 |
|
case self::OS_WINDOWS: |
71
|
|
|
shell_exec(sprintf('%s &', $this->command, $outputFile)); |
72
|
|
|
break; |
73
|
2 |
|
case self::OS_NIX: |
74
|
2 |
|
$this->pid = (int)shell_exec(sprintf('%s %s %s 2>&1 & echo $!', $this->command, ($append) ? '>>' : '>', $outputFile)); |
75
|
2 |
|
break; |
76
|
|
|
default: |
77
|
|
|
throw new RuntimeException(sprintf( |
78
|
|
|
'Could not execute command "%s" because operating system "%s" is not supported by '. |
79
|
|
|
'Cocur\BackgroundProcess.', |
80
|
|
|
$this->command, |
81
|
|
|
PHP_OS |
82
|
|
|
)); |
83
|
2 |
|
} |
84
|
2 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns if the process is currently running. |
88
|
|
|
* |
89
|
|
|
* @return bool TRUE if the process is running, FALSE if not. |
90
|
|
|
*/ |
91
|
1 |
|
public function isRunning() |
92
|
|
|
{ |
93
|
1 |
|
$this->checkSupportingOS('Cocur\BackgroundProcess can only check if a process is running on *nix-based '. |
94
|
1 |
|
'systems, such as Unix, Linux or Mac OS X. You are running "%s".'); |
95
|
|
|
|
96
|
|
|
try { |
97
|
1 |
|
$result = shell_exec("ps -ef | awk '{print $1}'"); |
98
|
1 |
|
$pidArray = explode(PHP_EOL, $result); |
99
|
1 |
|
if (in_array($this->pid,$pidArray) && !preg_match('/ERROR: Process ID out of range/', $result)) { |
100
|
1 |
|
return true; |
101
|
|
|
} |
102
|
|
|
} catch (Exception $e) { |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Stops the process. |
110
|
|
|
* |
111
|
|
|
* @return bool `true` if the processes was stopped, `false` otherwise. |
112
|
|
|
*/ |
113
|
1 |
|
public function stop() |
|
|
|
|
114
|
|
|
{ |
115
|
1 |
|
$this->checkSupportingOS('Cocur\BackgroundProcess can only stop a process on *nix-based systems, such as '. |
116
|
1 |
|
'Unix, Linux or Mac OS X. You are running "%s".'); |
117
|
|
|
|
118
|
|
|
try { |
119
|
1 |
|
$result = shell_exec(sprintf('kill %d 2>&1', $this->pid)); |
120
|
1 |
|
if (!preg_match('/No such process/', $result)) { |
121
|
1 |
|
return true; |
122
|
|
|
} |
123
|
|
|
} catch (Exception $e) { |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the ID of the process. |
131
|
|
|
* |
132
|
|
|
* @return int The ID of the process |
133
|
|
|
*/ |
134
|
1 |
|
public function getPid() |
135
|
|
|
{ |
136
|
1 |
|
$this->checkSupportingOS('Cocur\BackgroundProcess can only return the PID of a process on *nix-based systems, '. |
137
|
1 |
|
'such as Unix, Linux or Mac OS X. You are running "%s".'); |
138
|
|
|
|
139
|
1 |
|
return $this->pid; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set the process id. |
144
|
|
|
* |
145
|
|
|
* @param $pid |
146
|
|
|
*/ |
147
|
|
|
protected function setPid($pid) |
148
|
|
|
{ |
149
|
|
|
$this->pid = $pid; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return int |
154
|
|
|
*/ |
155
|
5 |
|
protected function getOS() |
156
|
|
|
{ |
157
|
5 |
|
$os = strtoupper(PHP_OS); |
158
|
|
|
|
159
|
5 |
|
if (substr($os, 0, 3) === 'WIN') { |
160
|
|
|
return self::OS_WINDOWS; |
161
|
5 |
|
} else if ($os === 'LINUX' || $os === 'FREEBSD' || $os === 'DARWIN') { |
162
|
5 |
|
return self::OS_NIX; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return self::OS_OTHER; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $message Exception message if the OS is not supported |
170
|
|
|
* |
171
|
|
|
* @throws RuntimeException if the operating system is not supported by Cocur\BackgroundProcess |
172
|
|
|
* |
173
|
|
|
* @codeCoverageIgnore |
174
|
|
|
*/ |
175
|
|
|
protected function checkSupportingOS($message) |
176
|
|
|
{ |
177
|
|
|
if ($this->getOS() !== self::OS_NIX) { |
178
|
|
|
throw new RuntimeException(sprintf($message, PHP_OS)); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param int $pid PID of process to resume |
184
|
|
|
* |
185
|
|
|
* @return Cocur\BackgroundProcess\BackgroundProcess |
|
|
|
|
186
|
|
|
*/ |
187
|
1 |
|
static public function createFromPID($pid) { |
188
|
1 |
|
$process = new self(); |
189
|
1 |
|
$process->setPid($pid); |
190
|
|
|
|
191
|
1 |
|
return $process; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.