douyong1886 2011-08-28 01:59
浏览 69
已采纳

PHP在动态时运行脚本? 为cron工作还是不工作?

I need to run scripts at dynamic times (coinciding with a datetime in a database). Any ideas how to accomplish this?

Example:

Database says tomorrow at noon in 1 in a datetime field, then i want to run the script tomorrow at noon.

  • 写回答

2条回答 默认 最新

  • duannaikuang1301 2011-08-28 02:03
    关注

    A very simple approach is to have a cron-job call a PHP script every 1 minute, and have it check the database for things that need done. If something needs done NOW(), then do it and remove from database.

    You need to consider things like locking, process stacking, multiple processes, etc... to make this robust. But if your need is simple, this is a simple way to make it work.

    Add this to crontab:

    * * * * *   /usr/bin/php /path/to/my/script.php
    

    And in /path/to/my/script.php

    <?php
    $ts = time();
    
    // Run for up to 50 seconds
    while($ts + 50 > time())
    {
       ... SELECT id, job_stuff FROM JobTable WHERE JobDate <= NOW() ...
    
       process job
    
       ... DELETE job_stuff FROM JobTable WHERE id = ...
    
       sleep(5);       
    } 
    

    Note, this is not robust. A robust script would grab a record while locked, update it to a "processing" status, process it, and update it to a "complete" status. This means that multiple processes could work at this same time (even if accidental) and not duplicate jobs. Also, this means that a single failure would not stop the train.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题