public static terminateByFilter ( string $cmd, string $gracefully = true ) | ||
$cmd | string | CMD used as the start |
$gracefully | string | optional Whether it should send SIGTERM rather than kill -9 forcefully |
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);
}
}
}
}
}