<?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();

 

 

相关文章:

  • 2022-12-23
  • 2021-05-16
  • 2021-11-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
相关资源
相似解决方案