public static function terminateByFilter($cmd, $gracefully = true) { $op = []; exec('ps x -o pid,command | egrep -v "ps x -o pid,command|egrep " | egrep ' . escapeshellarg($cmd), $op); if (!empty($op)) { foreach ($op as $str) { $pid = substr(ltrim($str), 0, strpos(ltrim($str), ' ')); if (!empty($pid)) { if ($gracefully) { posix_kill($pid, SIGTERM); } else { exec('kill -9 ' . $pid); } } } } }