[Solved] Why can’t a server be controlled without a client?


Why can’t a server be controlled without a client?

It can. A server may respond to the requests sent by the client but it can also do other things.

Ok, in all the years servers have existed, you’re telling me the best you can do is give me a crappy cron job if I want to make a script run every n seconds on my server?

I don’t know who said you that but that is simply not true. Even a shell script can do that.

There MUST be a way built into a programming language or something that lets me run a function or script or something at intervals without a client having to send a request to the server.

There must be and there is. See below for examples.

Is there any language that lets you do this?

Yes, there are many such languages. For example JavaScript had setTimeout() and setInterval() since something like 1995.

node js?

Yes, Node.js can do that and many other things. Since you asked specifically about Node.js – here are soem of the modules that you can use for that:

In fact there are many more modules that handle scheduled tasks.

How do big companies do server maintenance then?

There are many ways to do that, including the ones mentioned above. Sometimes they use cron, sometimes they use webhooks and other RPC to orchestrate large numbers of servers and initiate various tasks, sometimes the servers are doing it themselves. You can use stream, queue or pub/sub services in some cases to manage more complex scenarios.

Why can php listen for requests constantly and webhooks but can’t control the very thing it was designed to control without someone else sending a request?

Because PHP is not a standalone server. You can think of what you write in PHP as a kind of request handlers. And you need requests for the request handlers to, well, handle the requests.

In contrast, programs written in Node are standalone programs that keep running and can do what they want and when they want, and they just happen to respond to requests from the client. PHP scripts, on the other hand, are not standalone long-running programs but they are invoked by a server like Apache or nginx on every request to a specific route that is handled by a specific PHP script, and then the script runs, does its job of serving this single request and finishes, only to be invoked later for another request if it happens.

Now, if you want to have a periodically running PHP script then you can do something like this: put the script on the server, it can respond only to localhost if you want, and test it if it runs every time that you do e.g. a POST request to http://localhost:1234/script.php and if it does you can do few things to make it run every n seconds as you wanted:

(1) You can put curl -X POST http://localhost:1234/script.php in crontab

(2) You can make a shell script like this:

#!/bin/sh
while true; do
  curl -X POST http://localhost:1234/script.php
  sleep 10
done

(3) You can make a Node program like this:

#!/usr/bin/env node
const request = require('request');
setInterval(() => request.post('http://localhost:1234/script.php'), 10000);

(4) You can use any other language that lets you run standalone programs.

This is one way to have a PHP script that you want to be run periodically.

6

solved Why can’t a server be controlled without a client?