1

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 Answers2

0

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.

Tim Morton
  • 2,614
  • 1
  • 15
  • 23
  • Isn't exactly what i am looking for, but this will definitely help at a later date. Thanks for providing this. I'll update my question when I find my answer or figure it out. –  Jun 12 '19 at 00:12
  • This site is going to help https://web-push-book.gauntface.com/chapter-01/02-how-push-works/ –  Jun 12 '19 at 00:20
  • Probably your best bet is to set up a javascript timer that polls your website every 5 seconds or so, and when it gets a reply that says to go 'beep'. However, my experience is that playing sound in a browser is laggy. :( – Tim Morton Jun 12 '19 at 00:22
  • Get web push working, and you'll have knowledge that many programmers don't have :) – Tim Morton Jun 12 '19 at 00:23
  • While I couldn't exactly figure out the service workers, I did find the ajax polling. This will work for now. https://developer.hyvor.com/php/ajax-long-polling –  Jun 12 '19 at 02:03
-1

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

Kais Tounsi
  • 11
  • 4
  • 8
  • Hi, I've tried this already and this doesn't seem to work. https://stackoverflow.com/questions/24035090/run-cron-job-on-php-script-on-localhost-in-windows this is what i used to try and get it to work. But if it was to run every x minutes, I should be seeing that in my console log. –  Jun 11 '19 at 23:12
  • 1
    This cannot do what the OP is asking for. Typically, websites are a stateless transaction: the client asks for a page; the server responds with a page. After that, the transaction is done. No more communication can be sent to the client, and the client isn't looking for it anyway. – Tim Morton Jun 11 '19 at 23:53
  • @TimMorton so how does facebook auto notify you then when you are scrolling on their site. If someone likes my post, I automatically get the notification. There has to be a way that a script is being ran so that I am being notified. If I did an onclick statement from the other user's mouse click, how do I make the file run from my side of the site. The only way I see this happening is having a script run every 5 minutes that detects a change or a notification and then it notifies you. –  Jun 12 '19 at 00:04
  • good question, I'll have to research it. They're probably polling. – Tim Morton Jun 12 '19 at 00:10
  • They use push notifications. – Tim Morton Jun 12 '19 at 00:14