Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8236825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:15:43+00:00 2026-06-07T19:15:43+00:00

Is there a way I can automatically restart a PHP script whenever it exits,

  • 0

Is there a way I can automatically restart a PHP script whenever it exits, regardless of whether it has been exited properly, or has terminated due to an error, or maxed memory usage, etc.?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:15:44+00:00Added an answer on June 7, 2026 at 7:15 pm

    A PHP Script can also restart itself with PCNTL.

    Disclaimer: the point of this exercise is only to prove that PHP is perfectly capable of restarting itself and to answer the question:

    Is there a way I can automatically restart a PHP script whenever it exits, regardless of whether it has been exited properly, or has terminated due to an error?

    It is therefor beyond our scope to go into any detail about unix processes so I would suggest you start with the PCNTL book or refer to php-pcntl for more details. Developer discretion is required, just because we can, doesn’t mean it’s sensible. With all seriousness aside lets have some fun.

    In the examples we will assume it is a PHP CLI script launched in a *nix environment from a terminal with a semi-decent shell using the command:

    $ php i-can-restart-myself.php 0
    

    We pass a restart count attribute as indicator that the process was restarted.

    Can I automatically restart a PHP Script?

    Yes I can!

    <?php
        echo ++$argv[1];     // count every restart
        $_ = $_SERVER['_'];  // or full path to php binary
    
        echo "\n======== start =========\n";
        // do a lot of stuff
        $cnt = 0;
        while( $cnt++ < 10000000 ){}
        echo "\n========== end =========\n";
        
        // restart myself
        pcntl_exec($_, $argv);
    

    Regardless of whether it has been terminated properly?

    Yes I can restart if terminated!

    <?php
        echo ++$argv[1];    
        $_ = $_SERVER['_']; 
        
        register_shutdown_function(function () {
            global $_, $argv; // note we need to reference globals inside a function
            // restart myself
            pcntl_exec($_, $argv);
        });
    
        echo "\n======== start =========\n";
        // do a lot of stuff
        $cnt = 0;
        while( $cnt++ < 10000000 ){}
        echo "\n========== end =========\n";
        
        die; // exited properly
        // we can't reach here 
        pcntl_exec($_, $argv);
    

    Or terminated due to an error?

    Ditto!

    <?php
        echo ++$argv[1];    
        $_ = $_SERVER['_']; 
        
        register_shutdown_function(function () {
            global $_, $argv; 
            // restart myself
            pcntl_exec($_, $argv);
        });
    
        echo "\n======== start =========\n";
        // do a lot of stuff
        $cnt = 0;
        while( $cnt++ < 10000000 ){}
        echo "\n===== what if? =========\n";
        require 'OOPS! I dont exist.'; // FATAL Error:
    
        // we can't reach here
        echo "\n========== end =========\n";        
        die; // exited properly
        // we can't reach here 
        pcntl_exec($_, $argv);
    

    But you know I am going to want more than that right?

    Certainly! I can restart on signals kill, hub, and even Ctrl-C!

    <?php
        echo ++$argv[1];     
        $_ = $_SERVER['_'];  
        $restartMyself = function () {
            global $_, $argv; 
            pcntl_exec($_, $argv);
        };
        register_shutdown_function($restartMyself);
        pcntl_signal(SIGTERM, $restartMyself); // kill
        pcntl_signal(SIGHUP,  $restartMyself); // kill -s HUP or kill -1
        pcntl_signal(SIGINT,  $restartMyself); // Ctrl-C
    
        echo "\n======== start =========\n";
        // do a lot of stuff
        $cnt = 0;
        while( $cnt++ < 10000000 ){}
        echo "\n===== what if? =========\n";
        require 'OOPS! I dont exist.'; // FATAL Error:
    
        // we can't reach here
        echo "\n========== end =========\n";        
        die; // exited properly
        // we can't reach here 
        pcntl_exec($_, $argv);
    

    How do I terminate it now?

    If you flood the process by holding down Ctrl-C you might just catch it somewhere in the shutdown.

    I don’t want to see all these errors can I restart on those too?

    No problem I can handle errors too!

    <?php
        echo ++$argv[1];     
        $_ = $_SERVER['_'];  
        $restartMyself = function () {
            global $_, $argv; 
            pcntl_exec($_, $argv);
            // NOTE: consider fork and exiting here as error handler
        };
        register_shutdown_function($restartMyself);
        pcntl_signal(SIGTERM, $restartMyself);   
        pcntl_signal(SIGHUP,  $restartMyself);   
        pcntl_signal(SIGINT,  $restartMyself);   
        set_error_handler($restartMyself , E_ALL); // Catch all errors
    
        echo "\n======== start =========\n";
        // do a lot of stuff
        $cnt = 0;
        while( $cnt++ < 10000000 ){}
    
        echo $CAREFUL_NOW; // NOTICE: will also be caught
    
        // we would normally still go here
        echo "\n===== what if? =========\n";
        require 'OOPS! I dont exist.'; // FATAL Error:
    
        // we can't reach here
        echo "\n========== end =========\n";        
        die; // exited properly
        // we can't reach here 
        pcntl_exec($_, $argv);
    

    Although this appears to be working fine at first glance, because pcntl_exec runs in the same process we do not notice that things are terribly wrong. If you wanted to spawn a new process and letting the old one die instead, which is a perfectly viable alternative see next post, you will find 2 processes are started each iteration, because we trigger a PHP NOTICE and an ERROR due to a common oversight. This can easily be rectified by ensuring we die() or exit() after the call to pcntl_exec, since implementing an error handler PHP assumes tolerance were accepted and continues running. Narrowing the error reporting level instead of catching E_ALL will be more responsible as well.

    The point I am trying to make is that even if you are able to relaunch a script that failed this is not an excuse to allow broken code. Although there may exist viable use cases for this practice, I strongly disagree that restart on failure should be employed as a solution for scripts failing due to errors! As we can see from these examples there is no way of knowing exactly where it failed so we cant be sure where it will start from again. Going for the "quick fix" solution which could appear to be working fine may have more problems now then before.

    I would instead prefer to see the defect addressed with some proper unit tests which will help flush out the culprit so we may rectify the problem. If PHP runs out of memory it can be avoided through conservative use of resources by unsetting the variables after use. (btw. you will find assigning null is much quicker than using unset for the same result) I am afraid though, if left unresolved, restarting would most definitely serve as suitable opener to the can of worms you already have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way I can make it so my script will automatically close?
Is there any way in which I can automatically convert a Custom Class Object
Is there a way Emacs can automatically refresh the buffer showing the dvi right
Is there a way I can automatically change a string to the upper version.
In Lotus Notes 8.5, is there a way I can automatically search received emails
Is there any way can declare a bean in just like JSP UseBean in
Is there way that I can read the file from remote server using fopen
Is there any way I can set a formatter on models that will convert
Is there any way we can fetch X509 Public Cetrificates using c# from AD
Is there a way I can use one category to stylize all of my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.