How to run a php file every 5 minutes

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 answers

  • answered 2019-06-11 23:10 Kais Tounsi

    To create a scheduler in Windows, you need to: Create .bat file on your server or system; Type the following command in your .bat file:

    “F:\xampp\php\php.exe” -f “F:/xampp/htdocs/sitefolder/test.php”;
    

    Set the scheduler time and file in your task scheduler in Windows.

    Good Luck

  • answered 2019-06-12 00:08 Tim Morton

    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.