Skip to content

Instantly share code, notes, and snippets.

@jrivero
Last active February 21, 2024 11:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrivero/741973 to your computer and use it in GitHub Desktop.
Save jrivero/741973 to your computer and use it in GitHub Desktop.
Kill process if is modified
<?php
class Autokill {
var $start_mtime_included_files = array();
function Autokill() { $this->__construct(); }
function __construct() {
clearstatcache();
$files = get_included_files();
foreach ((array)$files as $file)
$this->start_mtime_included_files[$file] = filemtime($file);
}
function kill_if_modified() {
clearstatcache();
$modified = false;
$files = get_included_files();
foreach ((array)$files as $file) {
if (!array_key_exists($file, $this->start_mtime_included_files))
$this->start_mtime_included_files[$file] = filemtime($file);
else if (filemtime($file) != $this->start_mtime_included_files[$file]) {
echo $file."[".$this->start_mtime_included_files[$file]."<".filemtime($file)."] \n";
$modified = true;
break;
}
}
if ($modified)
$this->kill();
return $modified;
}
function kill() {
$pid = getmypid();
$cmd = "kill -9 ".(int)$pid;
echo $cmd."\n";
shell_exec($cmd);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment