swoole 监控文件改动

<?php

/**
 * 场景:
 * 进程监控文件改动
 */
date_default_timezone_set('PRC');

echo '进程id:' . posix_getpid() . PHP_EOL;
cli_set_process_title('php_c1');

$filepath = __DIR__ . DIRECTORY_SEPARATOR . 'a.txt';
if (!is_file($filepath)) {
    touch($filepath);
}

$child = new SwooleProcess(function (SwooleProcess $process) use ($filepath) {
    echo '子进程id:' . posix_getpid() . PHP_EOL;
    cli_set_process_title('php_c1_child');
    $watchMd5 = md5_file($filepath);
    while (true) {
        $getMd5 = md5_file($filepath);
        if (strcmp($watchMd5, $getMd5) !== 0) {
            echo date('[Y-m-d H:i:s] ') . $filepath . '被修改' . PHP_EOL;
            $watchMd5 = $getMd5;
        }
        sleep(3);
    }
}, true);

$child->start();

while (true) {
    $info = $child->read();
    if ($info) {
        echo $info;
    }
    sleep(3);
}

SwooleProcess::wait();
原文地址:https://www.cnblogs.com/cshaptx4869/p/11974354.html