r/PHPhelp • • 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.

5 Upvotes

12 comments sorted by

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.

→ More replies (1)

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'.

1

u/cdnBacon Oct 26 '26

Excellent points. Thanks.

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.

1

u/cdnBacon Oct 26 '26

This is likely what we will do. The more I read above, the more I think I have been looking at it backwards.

2

u/FreeLogicGate Oct 26 '26

is this sort of use case appropriate for a php daemon?

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.

Are there impediments I should be aware of in trying to run this from the commercial server?

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.

any pitfalls that I should be looking at?

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.

1

u/cdnBacon Oct 26 '26

Thanks for this ... very informative and a lot of information that was new to me.

I was envisioning an sql database as the repository, and I have much more experience with that than I do with server side programming at the level you are mentioning. And the number of fields is low and the queries not particularly complicated. So that side of things, I think I can handle.

My friend's teeny server has teeny tiny data capacity and is codeable on a bastardized version of C. Not sure how complicated it can get, but as I work through some of the responses here today, I am considering reversing the data flow so that instead of my commercial server intermittently requesting the data, his intermittently device sends it. Maybe encoded in the url (PUT / GET). There are only a half dozen fields to fill with each iteration. This has the advantage of minimizing storage on his device, as he could send it relatively quickly.

And it might get completely away from me having to schedule anything.

I really appreciate the comments. Thanks.

2

u/FreeLogicGate Oct 26 '26

Having the device call to the server is certainly a good model, but it will still need to schedule its callouts, and have the required support to make HTTP connections, and format data (ideally in JSON format).

In either case it sounds like you will need a sql database, so you want to make your decision on that sooner than later. Lots of people are hyped up about sqlite lately, and most of the newer development seems to favor postresql these days, but I'm partial to mysql or mariadb (which is a mysql fork -- long, boring story).

Talking to any of these with PHP is best done using PDO. PDO is sort of the PHP world equivalent of what ODBC used to be used for -- a generic interface to any kind of relational database or structured data source. Your server will want to have installed PDO and the driver for the database you are going to use.

These days, PHP projects should really be built upon and using the composer dependency management tool. In other words, your PHP project should be structured and designed around PHP namespacing rules. By using composer, you can also easily make use of libraries and frameworks which will save you a lot of time. For example, for any web project, symfony or laravel are widely used because they provide Model - View - Controller.

With that said, if you're looking for more of a walk through, then probably look at laravel, which will have enough examples, tutorials and documentation, that you're lest likely to get in over your head.

For visualization (graphs etc.) there are various ways to do that, but again there are probably libraries you can access via composer's repository (called packagist) which you can easily search. In summary, you want to avail yourself of component development.

Even if you don't go for a full MVC, but you do use a mysql server with PDO, I highly recommend installing/using Doctrine DBAL, which gives you a thin OOP wrapper around PDO, that still lets you concentrate on basic SQL statements, and fetching results into arrays. I'd also recommend at least using the Symfony HTTPFoundation component library, which in the PHP world is one of the most utilized components across projects, as it encapsulates HTTP for you, and removes the need to utilize any of the PHP superglobs. That component is here: https://symfony.com/doc/current/components/http_foundation.html

While there, you might want to look at some of the other components they created.

At that point your ingestion script can be very simple, and as you say, you can just have your server do a semi-restful api.

If it's just providing new data, than REST would say that your api should be sending a POST. You also will probably want to implement at least some simple form of authentication, so that if someone finds your url endpoint they don't start pumping it full of bogus data, looking for exploitation options. If the device has a static IP, you could also have the server whitelist it, again assuming that it's a VPS where you can set your own firewalling rules up.

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.