Search code examples
phpshutdown

Can PHP detect when the server is being shut down?


There is this situation:

  • Wampserver is running
  • A PHP script is running (set_time_limit(0), endless while loop)
  • The server is also used by an user (as, for example gaming PC).

Say the user wants to shutdown the computer, is there any way to detect this in PHP? So the user goes to Start > Shutdown.

If not, is there an easy way to be sure that there will be no problems with the script? (Like a shutdown between two queries that are both for the same object?)


Solution

  • register_shutdown_function() seems to be the one you are looking for. However, if the OS kills the script, before the shutdown code is completely executed, you cannot do anything from within the script.

    If not, is there an easy way to be sure that there will be no problems with the script? (Like a shutdown between two queries that are both for the same object?)

    Transactions, but they only ensure, that the data is not corrupted, not that the data is really written.

    My advice here: Use a dedicated system.