php 添加inotify扩展,php使用inotify扩展监控文件或目录,如果发生改变,就执行指定命令...

class Monitor

{

public $dir = ‘‘;

public $cmd = ‘‘;

public $timeout = 1;

public function __construct()

{

if (!extension_loaded(‘inotify‘)) {

echo ‘请安装inotify扩展‘, PHP_EOL;

exit;

}

//解析命令行参数,有一个:号表示必填项

$opts = getopt(‘‘, [‘dir:‘, ‘cmd:‘]);

if (!$opts) {

echo ‘参数输入错误‘, PHP_EOL;

exit;

}

if (empty($opts[‘dir‘])) {

echo ‘--dir 是必填项‘, PHP_EOL;

exit;

}

if (empty($opts[‘cmd‘])) {

echo ‘--cmd 是必填项‘, PHP_EOL;

exit;

}

$this->dir = $opts[‘dir‘];

$this->cmd = trim($opts[‘cmd‘]);

$this->run();

}

//对目录进行监控

public function run()

{

$dirs = $this->getDirs($this->dir);

if (empty($dirs)) {

return false;

}

$fd = inotify_init();

//设置为非阻塞模式

stream_set_blocking($fd, 0);

foreach ($dirs as $dir) {

$watch = inotify_add_watch($fd, $dir, IN_MODIFY | IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_CLOSE_WRITE);

if (!$watch) {

echo "{$dir} 添加监控错误", PHP_EOL;

exit;

}

}

while (true) {

$reads = [$fd];

$write = [];

$except = [];

if (stream_select($reads, $write, $except, $this->timeout) > 0) {

if (!empty($reads)) {

foreach ($reads as $read) {

//文件改变

$fileChg = false;

//目录改变

$dirChg = false;

//从可读流中读取数据

$events = inotify_read($read);

$fileName = ‘‘;

foreach ($events as $event) {

$fileName = $event[‘name‘];

switch ($event[‘mask‘]) {

case IN_CREATE:

case IN_DELETE:

$fileChg = true;

break;

case 1073742080:

case 1073742336:

$dirChg = true;

break;

}

}

if ($fileChg) {

echo "文件 {$fileName} 发生改变, 执行命令 {$this->cmd}", PHP_EOL;

echo shell_exec($this->cmd), PHP_EOL;

}

if ($dirChg) {

echo "目录 {$fileName} 发生改变, 执行命令 {$this->cmd}", PHP_EOL;

echo shell_exec($this->cmd), PHP_EOL;

return $this->run();

}

}

}

}

}

return true;

}

//递归的获取当前目录下所有子目录路径

public function getDirs($dir)

{

$dir = realpath($dir);

$dh = opendir($dir);

if (!$dh) {

return [];

}

$dirs = [];

$dirs[] = $dir;

while (($file = readdir($dh)) !== false) {

if ($file == ‘.‘ || $file == ‘..‘) {

continue;

}

$full = $dir . DIRECTORY_SEPARATOR . $file;

if (is_dir($full)) {

$dirs = array_merge($dirs, $this->getDirs($full));

}

}

closedir($dh);

return $dirs;

}

}

(new Monitor());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值