Last active
January 28, 2020 19:58
-
-
Save chrisribe/589dc79ce72018c3625ac3be97681732 to your computer and use it in GitHub Desktop.
PHP kill / terminate process by exe name
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
/** | |
* Kill windows process if found and older that given time (defaults to now) | |
* Ref: https://stackoverflow.com/questions/14385122/how-to-kill-a-windows-process-running-longer-than-30-minutes | |
*/ | |
function killProcessByName($processName, $ifOlderThan = "now"){ | |
//Build datetime formated for "wmic process CreationDate" | |
//Format: year-month-day-hour-minute-second-microsecond-timezone (in minutes!!!) | |
$dt = new DateTime($ifOlderThan); | |
$t = $dt->format('YmdHis.u') . ($dt->format('Z') / 60); | |
//wmic query to terminate process by name if creationdate less than | |
$query = "wmic process where \"name='$processName' AND CreationDate < '$t'\" call terminate"; | |
ob_start(); | |
passthru($query . " 2>&1"); | |
$output = ob_get_contents(); | |
ob_end_clean(); | |
if(strpos($output, "ReturnValue = 0") !== false){ | |
return true; | |
}else{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment