#!/usr/bin/env php $entries The names in /proc. * @param int $self The pid to leave out. * * @return int */ function ss_unix_processes_count_proc(array $entries, int $self) : int { $count = 0; foreach ($entries as $entry) { if (ctype_digit($entry) && (int) $entry !== $self) { $count++; } } return $count; } /** * Counts the process lines in 'ps ax' output. * * A process line carries a TIME column with a colon and the header does * not. The listed pids, this process and its ps child, are left out; the old * 'ps ax | grep -c :' pipeline counted its own shell, ps and grep. * * @param string $ps The output of 'ps ax'. * @param array $exclude Pids to leave out. * * @return int */ function ss_unix_processes_count_ps(string $ps, array $exclude) : int { $count = 0; foreach (explode("\n", $ps) as $line) { if (!str_contains($line, ':')) { continue; } if (preg_match('/^\s*(\d+)\s/', $line, $matches) && in_array((int) $matches[1], $exclude, true)) { continue; } $count++; } return $count; }