1

I'm running a php script via cli, so it can run in background. This script is suposed run 24/7. How can I prevent it from shutting down (on errors, warnings, etc) and restart immediatly if it happens?

Thanks in advance!

MGP
  • 653
  • 1
  • 14
  • 33

1 Answers1

2

You could use a shell script to call php in an infinite loop, and run the shell script in the background. For example (a bit simplistic) a script like this, say "runloop.sh":

#!/bin/bash
# Run php script in a loop
while true; do
  php phpscript.php
done;

...and then run that script in the background, or from init. From the command prompt:

$ ./runloop.sh &

...to run the script in the background. It should run forever, unless you kill it somehow.


I should add that you'll need to make the shell script executable:

$ chmod +x runloop.sh
Dmitri
  • 9,175
  • 2
  • 27
  • 34
  • So that loop will only execute that script only if it's not running already, right? – MGP Mar 01 '12 at 17:37
  • as long as you only run one copy of the shell script. It will call php, wait for it to finish (possibly from an error), and then run it again... over and over until you kill the script. – Dmitri Mar 01 '12 at 17:39
  • Thank you @Dmitri! You gave me an idea how to restart reactphp app from inside docker container without using docker healthcheck setting (we can't use it). Instead of a simple loop we used loop with custom retry strategy. – Vlad Moyseenko Mar 22 '23 at 17:20