dongqiuwei8667 2019-06-11 23:07
浏览 82
已采纳

如何每5分钟运行一个php文件

How do I run a PHP file every 5 minutes or so? I've attempted to use cron, but it doesn't seem to be working. Say I wanted my site to run a php file that plays a sound. How do I make it so that the php file is ran every 5 minutes, so that the sound is played every 5 minutes. I know JavaScript can make it so the function can be run at a set interval, but I want the file to do the same.

Reason for this is that I'm creating a site and I want the users to be notified live. In order to do this, I need the php file to run so it can detect changes for users notifications and then make the noise if the user gets a new notification. If more info is needed, I can provide.

Edit:

So here is what I used to try and run a cron localhost Run Cron Job on PHP Script, on localhost in Windows

My script.bat file "C:\Xammp\php\php.exe" -f "C:\Xammp\htdocs\SocialMedia\Admin\Users\Notification.php"

And my shellscript.vbs file

Set WinScriptHost = CreateObject("WScript.Shell") WinScriptHost.Run Chr(34) & "C:\Xammp\htdocs\SocialMedia\Site\script.bat" & Chr(34), 0 Set WinScriptHost = Nothing

I've followed what they said in that post, but I don't see any change in my console log for networks. It should show a file being ran every x minutes but it doesn't

Edit: Although my question isn't completely answered, this site is where I found my answer. https://web-push-book.gauntface.com/chapter-01/02-how-push-works/ Thanks!

Edit - This was the answer to my question - https://developer.hyvor.com/php/ajax-long-polling

  • 写回答

2条回答 默认 最新

  • dongzhi1904 2019-06-12 00:08
    关注

    Running a cron job in linux or task scheduler in windows will not accomplish what you are wanting. That's not the way the technology works. Client asks question, server replies, end of story. To illustrate the point, how does your cron job know who is supposed to get updated? It doesn't, and it can't.

    The only way I can think of in a typical website scenario is to have your client continually polling the server for updates via ajax. If your audience is not large, this would probably work. But it will not scale well.

    There is another technology that might work for you: web push notifications.

    However, I suspect that there is a rather large learning curve associated with utilizing this concept. A quick search revealed some pages that you will want to investigate:

    https://serviceworke.rs/push-get-payload_demo.html

    https://github.com/web-push-libs/web-push-php

    I really know nothing beyond the name, so I don't know if this will do what you are asking for, but it's the only thing other than having your client regularly poll the server.

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

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢