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