Take the 2-minute tour ×

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!

share|improve this question
Run it for an hour and every hour. –  Alice Mar 1 '12 at 17:10
Sounds it like cronjob job? –  Ismael Mar 1 '12 at 17:14
Call it from a shell script, in an endless loop? –  Dmitri Mar 1 '12 at 17:16
@Dmitri Wont that run "infinite" versions of the same script? –  MGP Mar 1 '12 at 17:24
@Alice But what if the script stops after 2min, with some kind of error? –  MGP Mar 1 '12 at 17:25
show 4 more comments

1 Answer

up vote 1 down vote accepted

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":

# Run php script in a loop
while true; do
  php phpscript.php

...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
share|improve this answer
So that loop will only execute that script only if it's not running already, right? –  MGP Mar 1 '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 1 '12 at 17:39
Thats exactly what i need :) thanks a lot –  MGP Mar 1 '12 at 17:43
add comment

Your Answer


