How to create daemon and infinity looping – PHP – SitePoint Forums

Hello family,

Please I need guide on the best way to approach this for better results.

I have a html file called monitor.html that uses JavaScript to check value of .teatime every seconds. And if the val is 26 it will make an ajax call to a php file that will do something for me.

My Challenge:

Everything works fine as long as i am viewing that monitor.html on my laptop browser it works and everything works perfectly, but i don’t intend opening the monitor.html in a browser let alone staty glued to my computer to watch it.

I want a solution where i can host that monitor.html file on a server which will act as if am viewing it on a browser or which will make the JavaScript timing to keep working and ajax call made anytime the val is 26.

I have this three methods in mind:

  1. Create an infinity while loop on my server with a sleep of 1secs that will keep redirecting to monito.html url but not sure if this will make the JavaScript to work or this can be very heavy on my server.

  2. Using infinity while loop to check the data every 1secs and execute the functions anytime value is 26, but the first approach uses a btc price ticker JavaScript so html works fine, but if i want not to use JavaScript it means i have to be calling the price ticker api every 1secs to fetch data and i don’t have have the api yet.

  3. Simple look for a virtual server or any remote server that i can upload the monitor.hrml file so everything will be running there including the price ticker and my custom JavaScript functions, and make ajax call when ever the val is 26.

Note i don’t want to stay glue to my laptop, i just want it to work even if i did not turn on my computer or visit the page it will continue running and executing.


Does the JS have to be in the monitor.html file or can it be a stand alone JS file? Does the JS file read anything out of the HTML pages DOM or anything? If not, you can always run the code in Node.js and set it up to be a background task. That or put it on a remote Node.js server and have it run as a program.

You could also just write the code in a language like PHP itself and then have a job scheduler hit the URL every so often (through like a curl command in a cron job) and have it then call that other PHP page.

Your main problem is that you used a client side script to try and run something that probably should have been done in a server-side script. Not impossible to do, but if you might want to look at something like running the script in Node.js so you can put it on some other server and have it run continuously. That or put Node.js on your own machine and then tell your OS to run Node.js with your script and let it run in the background.


It sounds to me that a websocket or cron server should do the trick. I have played with cron server with success. In production exporting data from database each night.

I don’t want to use cron because of how accurate and fast I want the triggers to be.

This explains what am doing.

I have this project and it contains just three files


Now here is the work flow

// view.html
<script src=""></script>
<div class="binanceprice"></div>
// myjava.js
let ws = new websocket ("");
ws.onmessage = (event) => {
$('.binanceprice').innerHtml =;
var monitor = setInterval(
var getprice = $('.binanceprice').text();
if (getprice > 18600){
url: 'process.php'
), 500});
// process.php
add to database or do something 

Now as long as i am visiting view.html on my laptop, with my internet connection active and stable and my browser is on and am sitting there watching or even leaving it one that way waiting for the price to hit 18500 for a trigger to occur and call the process.php
It works but I can’t sit all day waiting for the price to hit target and my ajax is triggered, so how can i create or something that will act as though my laptop is on or browser or anything that acts as a virtual browser or machine that will run view html url and execute all JavaScript functions for me without me having to turn on my computer or connect to the internet and wait in front of the screen

If you use Go, C, C++ or Rust, this may not be a problem. A compiled server is faster than an interpreted server like PHP, Javascript, Python etc.

something like this is what i have in mind, but is like is only for the js file? what happens to the view.html where most things are needed?

is it possible to run html too in a virtual server or machine?

i really do not know how to code in those languages, i don’t even know how to host them but if it can actually do exactly what i have in mind with the speed as fast as 200ms then i can look into it too

JavaScript seems fast with even 100ms and it won’t hurt your server.
but am yet to know of any html virtual browser or server that can do that.

secondly, using php to hit my server per secs in and infinity looping is something I think may hurt my server. beside binance api have limits but there websocket streaming is best with realtime fetching of data.

and thinking of redirecting or posting to view.html through php in an infinity loops i don’t know if the JavaScript and ajax and other js functions will work properly like when real person is actually viewing the page in his computer

Yes it reads something from the database, just some information that is printed once like table colum id.

but if there is a way i can query my database once and get those details and store as a JavaScript static variables.

then i can run streaming for binance and if target is hit i can then ajax call the process.php

by doing this i don’t need or won’t be needing the html files.

but then i only want the information from the database outputted once so i don’t have to be querying my database every secs for a data that does not change , that is the reason i opted for using html but still can’t automate it

I can only speak for Go. You do not have to learn more than to do the task.

A Go compiled cron server is about 10MB in size. Upload a single executable and run this command in the terminal :

cd /path to go folder for executable (single file)
./main (name of the Go executable)

No dependencies. No Apache. Just start the executable.

1 Like

I think I should be looking at Node js instead of Go

The main reason is stated in the comparison link you shared, hard to get developers, learning new language and still not widely known like node js

But if i choose to go with Node js does it mean i have to change my JavaScript code? or everything still remains the same ?

I have to pass this. But can you use Vanilla Javascript in Node, I doubt that you have to do major changes.

1 Like

okay thanks, let me study it and see how easily i can implement it, and then i will revert back here if i encounter any issues

Source link


Leave a Reply

Your email address will not be published. Required fields are marked *