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 \Swoole\Process(function (\Swoole\Process $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);
}

\Swoole\Process::wait();

 

 

posted @ 2019-12-03 01:02  白開水  阅读(454)  评论(0编辑  收藏  举报