Last active
January 21, 2020 21:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?#!/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" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this does not work!
1.) php_uname('s') returns also "Darwin" on macOs
and second this is just cluttered code