【问题标题】:Convert this to run as a cron?将其转换为作为 cron 运行?
【发布时间】:2012-04-26 21:29:48
【问题描述】:

有没有办法可以将此函数转换为从 cron 作业运行,然后将它返回的数据插入数据库?

public function ping($host, $port=25565, $timeout=0.1) {
//Set up our socket
    $beginning_time = microtime(true);
    $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
    if (!$fp) return false;
    $end_time = microtime(true);

//Send 0xFE: Server list ping
    fwrite($fp, "\xFE");

//Read as much data as we can (max packet size: 241 bytes)
    $d = fread($fp, 256);

//Check we've got a 0xFF Disconnect
    if ($d[0] != "\xFF") return false;

//Remove the packet ident (0xFF) and the short containing the length of the string
    $d = substr($d, 3);

//Decode UCS-2 string
    $d = mb_convert_encoding($d, 'auto', 'UCS-2');

//Split into array
    $d = explode("\xA7", $d);

//Return an associative array of values
    return array(
        'motd'        =>        $d[0],
        'players'     => intval($d[1]),
        'max_players' => intval($d[2]),
        'latency'     => ($end_time - $beginning_time) * 1000);
}

它返回的数据是数组末尾的数据。

【问题讨论】:

  • 错误...是的。您只需编写一个调用该函数的脚本,然后使用它通过 PDO 返回的数据。然后设置一个 cron 作业来调用该脚本。
  • 我对 PHP 和其他东西有点陌生,这将如何完成?
  • @unlucky4ever - 第一次让脚本在命令行上运行并产生所需的效果。第二,查找crontab的手册页。
  • 您在这里似乎有两个完全不同的问题。 “我如何在 PHP 中处理 SQL 数据库?”和“如何设置 cron 作业?” ... 找到介绍性教程然后就 SO 提问可以更好地回答这两个问题。

标签: php mysql cron


【解决方案1】:

您可以从命令行运行任何 PHP 文件。因此,可以在 cron 中使用相同的命令。无需转换任何东西。

/usr/local/bin/php /home/test.php

/usr/local/bin/php 路径应该是你的 php 二进制文件的位置。

【讨论】:

  • 好的,谢谢,我应该多研究一点...我现在可以看到我将如何制作它以便插入数据等等,但是我以前从未做过 cron 工作,我该怎么做去设置一个?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-07
  • 2015-02-06
  • 2012-10-17
  • 1970-01-01
  • 2017-12-23
  • 1970-01-01
  • 2014-09-22
相关资源
最近更新 更多