Skip to content

Instantly share code, notes, and snippets.

@bulton-fr
Created October 4, 2017 12:46
Show Gist options
  • Save bulton-fr/74916c1905eb6ed4d104086445cc6a73 to your computer and use it in GitHub Desktop.
Save bulton-fr/74916c1905eb6ed4d104086445cc6a73 to your computer and use it in GitHub Desktop.
Search and get all runned process with a name filter
<?php
$exec = shell_exec('ps ax|grep myProcessName');
$execList = explode("\n", $exec);
$processList = [];
foreach ($execList as $processLine) {
$matches = [];
if (preg_match('/(\d+) (.*) (S.*) (\d+:\d+) (.*)/', $processLine, $matches) === false) {
continue;
}
if (!isset($matches[1])) {
continue;
}
// PID TTY STAT TIME COMMAND
$processInfos = (object) [
'pid' => trim($matches[1]),
'tty' => trim($matches[2]),
'stat' => trim($matches[3]),
'time' => trim($matches[4]),
'cmd' => trim($matches[5])
];
if ($processInfos->cmd !== '') {
$processList[] = $processInfos;
}
}
var_dump($processList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment