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();
分情破爱始乱弃,流落天涯思别离。
如花似玉负情意,影如白昼暗自迷。
随风浮沉千叶落,行色匆匆鬓已稀。