donglv6747 2013-06-10 22:30
浏览 24
已采纳

交互后运行php脚本x小时(cronjob)

In my php script, I want a function to be executed 2 hours after an user has registered, whether he is logged in or not. I know it has somehow to be done with cronjob, but I have never worked with it before and I don't know how to pack this all together.

How would I go about this? I have looked around for answers but I haven't found a solution. My first idea was to store timestamps in a database and collect them with a query. A cronjob would then have to be run every few minutes to keep things updated. Would that be the right way? I would also appreciate some useful links for getting into cronjobs easily. Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dsgw8802 2013-06-10 23:20
    关注

    Yes that is how I would do it.

    From command line:

    crontab -e
    

    Then enter the following on a new line:

    * * * * * php /path/to/your/cron/script.php
    

    This will run script.php every minute

    You cron script would be something like follows:

    $qry = "SELECT * FROM users WHERE signup_date < '$two_hours_after_now' AND cron_run = '0'"
    // stuff you want to do
    $qry = "UPDATE users SET cron_run = '1' WHERE id = '$id'";
    

    Hopefully this will point you in the right direction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀