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.

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站