Skip to content

Instantly share code, notes, and snippets.

@lsauer
Last active January 21, 2020 21:30
Show Gist options
  • Save lsauer/6476476 to your computer and use it in GitHub Desktop.
Save lsauer/6476476 to your computer and use it in GitHub Desktop.
PHP: kill a process by its process-id/pid in a platform- and library-independent manner
<?#!/usr/bin/php5
# www.lsauer.com, 2012 lo sauer
# desc: kill a process on Linux, MacOS, Windows without a process-control library
# in the php setup or environment
$kill = function($pid){ return stripos(php_uname('s'), 'win')>-1
? exec("taskkill /F /PID $pid") : exec("kill -9 $pid");
};
//e.g.
echo $kill(19008);
//> "Successfully terminated...."
array_map($kill, [19008,23012,1802,930]);
//killall: without using array_map and a boolean return value
$killall = function($pids){ $os=stripos(php_uname('s'), 'win')>-1;
($_=implode($os?' /PID ':' ',$pids)) or ($_=$pids);
return preg_match('/success|close/',
$os ? exec("taskkill /F /PID $_") : exec("kill -9 $_"));
};
if( $killall([19008,23012,1802,930]) and $killall(19280)){
echo "successfully killed all processes"
}
@brunojennrich
Copy link

this does not work!

1.) php_uname('s') returns also "Darwin" on macOs

and second this is just cluttered code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment