0

i'm creating a socket server with ReactPHP and i need it to run forever. I also have a command panel where i have to check if the process is running, and i can stop or start it (or restart it).

I don't know howe to achieve this.

My plan was:

  • With play button: start the php command shell_exec with simply "php script.php".
  • With stop button i can do in 2 ways: 1. i can set in the loop a timer that every 5 seconds checks if there is file inside the folder (like "stop.lock") and then stop the process. 2. i can save the process PID in the database, and so clicking the stop button i can just kill the process.
  • Checking online status: I can make another script that tries to connect to the IP/port and if succeeds is online, if not (timeout 5 seconds) is offline.

I also want the script stay always in the listening status, so how can i make the script auto-start if for example i have to restart my server? I was thinking about a cron trying to connect to the server every minutes; and if it fails, it will just lauch again shell_exec('php script.php');

How is the best solution to handle all? (Server OS is CentOS 7)

StefanoV827
  • 269
  • 6
  • 18

2 Answers2

0

As @Volker said, just stop the loop if you want to stop it gracefully. You could check periodically a file or query a table but that's not a great way.

A nice flow could be to listen to an admin message to stop the server. Of course, you should care of authenticating who can stop the server. This way it will stop without having to wait an interval to run, and you reduce the overhead of querying periodically your filesystem or your database.

Another cool way could be using RabbitMQ or a similar queue service. You just listen to your queue server, and you can send a message from your script to RabbitMQ, and from there to your server.

Good luck!

Edit: If you are running your server with systemd, a great way of handling it could be just to listen to a system signal to gracefully stop the application. Take a look at addSignal, you can handle a kill by pid, but also through systemd.

Dev_NIX
  • 163
  • 4
  • 18
  • Oh that's amazing! Thank you for sharing RabbitMQ! I didn't know about that. So i can't just handle a stop message and just stop the server. What about checking online status? Maybe if i do a service running with systemctl, i can use: "sudo systemctl enable myservice", "start" to run it, and "status" to check the status. But if i want to make this service from a php panel instead of manually from ssh, can I? – StefanoV827 Apr 10 '21 at 23:16
  • Let's divide this: you *can* listen to a message and stop the server, but you want to allow that message only to an authorized client to prevent a malicious message to stop your server. RabbitMQ is just another way to do it, stopping the server remotely. If you want to stop it from a script, you can listen to a SIGTERM signal with addSignal, and launch a "system myservice stop" command from a PHP script. For the online status, you can just open a connection to your server if TCP (or send a PING command from UDP) and if the connection is successful or the ping is responded, your server is alive – Dev_NIX Apr 12 '21 at 07:26
0

To handle graceful shutdown versus long running streamed response I've created a acquire/release like mechanism.

When a handler starts streaming a long response it acquires a lock and when streaming is done it releases it (it's just an array of uniqid()).

The server can decide to wait if there is active locks.

I use supervisor to handle the start/stop with a SIGTERM signal.

quazardous
  • 846
  • 10
  • 15