r/PHPhelp • u/cdnBacon • Oct 26 '26
Hobbyist here, with a question about PHP daemons on a server ....
So my friend and I are patching together an old student weather station and I have been tasked with handling the data. It comes from his teeny-tiny server on the station as frequently as every 2 seconds (clearly way too granular for weather data ...) and I am writing the webpage and data visualization. My vision is to poll his server every X minutes and build a data set from the responses, then manipulate the data to create appropriate reports and visualizations for any users.
I should emphasize ... this is play. Pure hobby work.
Most of the coding described above is I have done before. However, I have not set up a continuous polling script, let alone on a paid server (Bluehost, in this case).
So I am reading about php daemons, which sound like exactly the sort of thing I need, and seem quite simple, frankly ... but most of the examples I find seem to be run on desktops for short periods, rather than on web servers proper for, well, an unlimited time period.
My questions:
- Is this sort of use case appropriate for a php daemon?
- Are there impediments I should be aware of in trying to run this from the commercial server?
- Any pitfalls that I should be looking at?
I know that this is higher level ... I haven't written any code for the daemon yet besides playing around a bit on my localhost. Just making sure I am on the right general track before getting formally started.
Thank you for any insights.
18
u/St1ck0fj0y Oct 26 '26
If you only need to send a request and store the response data, you can keep things really simple actually.
All you need is a simple script (even a single file would do) and set a cronjob on your server to execute that script every x minutes.
No need to keep the script alive/running all the time, like a daemon. A cronjob is enough for your use case.
5
u/vincent_sch Oct 26 '26
I also think cron is enough here. But first you should check if requests from Bluehost get to your friend's server at all. If the station is on a school or home network, the router probably blocks them unless he forwards a port. In that case you could do it the other way around. The station sends its data to a PHP page on your site every few minutes and that page saves it. Then you also don't need cron.
If you poll from cron, you should set a timeout of about 10 seconds on the request. PHP on the command line has no time limit. So if the station stops answering, the script could still be waiting when cron starts the next one. Also PHP doesn't switch to your script's folder when cron runs it. For files you read or write, you should use paths like
__DIR__ . '/data.csv'.3
u/Intrepid-Ad-2306 Oct 26 '26
i’d definitely do it the other way around. just have the server on the weather station push the data to the web server and store that for processing. that’s typically how remote data collection is done.
if you really want to do the polling the other way, just schedule a cron job and avoid the headache of the daemon.
2
u/FreeLogicGate Oct 26 '26
No, for multiple reasons. You are describing a client script that acquires data from an api. So what is required is a client library that can make the network connection. You didn't specify what the format of the data from the device is, so I can't say much more. The other thing about PHP, is that PHP scripts have no persistent or shared state. People used to say that PHP has "script scope" which is another way of saying that PHP scripts run, allocate memory, compute, etc. and then exit as soon as possible. This model is well suited to CGI/serverside scripts run by a web server on a per/request basis. In your case, your php code is going to be run by the PHP cli application, and not through a webserver.
Possibly. You need hosting that allows you to set up the scheduled run of your client script. Super low cost shared hosting is typically very restrictive. You may at minimum want to look into a VPS. Assuming you are going to run this all on one server (including the visualization) you need enough memory and resources to support all the components your application will need.
A few. You need a scheduler to run your client ingestion script. I saw other comments mention cron. Cron is no longer the way to schedule jobs like this. The reason for this, is that the major linux distros have adopted to integration of SystemD. The job of SystemD in linux, is to install itself as the process that manages the startup and monitoring of all other services. So in the past, where people would typically create a shell script and use cron to schedule it, now you are advised to create a SystemD service file for your client. To do this you have to learn a bit about making a systemD service file and where to place it, and your service file will be configured to run once and exit, unlike the many daemon services which are designed to persist. With a valid working service file, you can then add a systemD timer file, that will do the actual scheduling. Once you've done this, you will be able to use the systemD management programs (systemctl, journalctl) to configure and monitor your client script.
What remains is the question of how your application will store the data. Typically, you would want to use some sort of relational database (sqllite, mysql/mariadb, postgresql) so your ingestion script would want to insert new row(s) in the database. Having a competent database design will facilitate whatever functionality you envision in the visualization application.
2
u/Bluehost_Support_Lyn Oct 27 '26
hey u/cdnBacon, thanks for sharing your project! For what you're describing, a cron job is usually the best fit. It can run every few minutes, collect the data, save it, and then exit cleanly. That's typically much simpler and more resource friendly than keeping a PHP daemon running continuously.
Based on your setup, it also sounds like having the weather station send data to your site could work well and remove the need for polling altogether. Sounds like a fun project, and you're definitely on the right track.