Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit d139738

Browse files
committed
Make process checks work better
As reported in #82, on some operating systems, `ps -ef` will not work as intended if the window is too small to show the entire process name. The solution to this is to enable unlimited width output, which causes each line to wrap (`-ww`). This patch also does method extraction refactors to simplify the logic.
1 parent 9a52cda commit d139738

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

src/Check/ProcessRunning.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class ProcessRunning extends AbstractCheck
2020
* @var string
2121
*/
2222
private $processName;
23+
24+
/**
25+
* @var int
26+
*/
2327
private $pid;
2428

2529
/**
@@ -45,14 +49,14 @@ public function __construct($processNameOrPid)
4549
}
4650

4751
if (is_numeric($processNameOrPid)) {
48-
if ((int)$processNameOrPid < 0) {
52+
if ((int) $processNameOrPid < 0) {
4953
throw new InvalidArgumentException(sprintf(
5054
'Wrong argument provided for ProcessRunning check - ' .
5155
'expected pid to be a positive number but got %s',
52-
(int)$processNameOrPid
56+
(int) $processNameOrPid
5357
));
5458
}
55-
$this->pid = (int)$processNameOrPid;
59+
$this->pid = (int) $processNameOrPid;
5660
} else {
5761
$this->processName = $processNameOrPid;
5862
}
@@ -65,17 +69,35 @@ public function check()
6569
{
6670
// TODO: make more OS agnostic
6771
if ($this->pid) {
68-
exec('ps -p ' . (int)$this->pid, $output, $return);
72+
return $this->checkAgainstPid();
73+
}
6974

70-
if ($return == 1) {
71-
return new Failure(sprintf('Process with PID %s is not currently running.', $this->pid));
72-
}
73-
} else {
74-
exec('ps -ef | grep ' . escapeshellarg($this->processName) . ' | grep -v grep', $output, $return);
75+
return $this->checkAgainstProcessName();
76+
}
7577

76-
if ($return == 1) {
77-
return new Failure(sprintf('Could not find any running process containing "%s"', $this->processName));
78-
}
78+
/**
79+
* @return \ZendDiagnostics\Result\ResultInterface
80+
*/
81+
private function checkAgainstPid()
82+
{
83+
exec('ps -p ' . (int) $this->pid, $output, $return);
84+
85+
if ($return == 1) {
86+
return new Failure(sprintf('Process with PID %s is not currently running.', $this->pid));
87+
}
88+
89+
return new Success();
90+
}
91+
92+
/**
93+
* @return \ZendDiagnostics\Result\ResultInterface
94+
*/
95+
private function checkAgainstProcessName()
96+
{
97+
exec('ps -efww | grep ' . escapeshellarg($this->processName) . ' | grep -v grep', $output, $return);
98+
99+
if ($return > 0) {
100+
return new Failure(sprintf('Could not find any running process containing "%s"', $this->processName));
79101
}
80102

81103
return new Success();

0 commit comments

Comments
 (0)