-1

I have a PHP script running permanently on a server. I have very limited access to restart it. Is there any way how to reset everything in the script from inside? I do not mean to physically stop and start the script.

I'm looking for a way how to almost restart it (unset() all variables, un include all includes, "forgot" all functions) almost like a restart of the script.

The reason is to be able to "update" the script at least during the debugging phase. On a local host everything works as it supposed, but on a server there are some issues and for any change I have to call to a service desk). Lets say to make some changes in the include - then include it again, that means to initialize new functions, activate new variables....

I know that restart would be easier, nicer, crone would do the job, but unfortunately there is not the option... (Stupid situation, I know - but I can not change it, so looking for solution within my reach)

The idea is

<?
while (true) {
  require(myInclude.php);

  while (true){
    // here is the code I need to process, it is in endless loop as it serves as a websocket server
    doWhatIsNeeded(this_works_fine_no_need_to_adjust);

    // condition that starts all the "cleaning"
    if ($restartIsRequired == true){
      break;
    }
  }
  //unset all variables, un include all includes, "forget" all functions
  // **************************************************************
  // ****** here this is the part, I would like to help with ******
  // ******                     thank you :)                 ******
  // **************************************************************

}
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
Láďa
  • 5
  • 4
  • 1
    It seems you could use a cron job but most of the times there's an alternative, I simply didn't understand, could you please provide more info and the actual code. – Noé Nov 26 '21 at 20:14
  • What exactly do you mean by "unset a function", and why exactly will you need to do so? It's a bit unclear what the actual problem is that you're trying to solve here. What exact issue are you facing due to the script running permanently? – ADyson Nov 26 '21 at 20:26
  • Thanky guys, cron would be perfect, unfortunetly it is not a solution in this case (stupid case)... The actual code has (include includes :) above 8000 lines... so I have written here some kind of an example... Thank you @Noé and ADyson If you will have a moment to check the updated question, it might make the situation more clear. – Láďa Nov 27 '21 at 10:40
  • Ok. So, under what conditions would you want to "reset" or "update" it, as you describe? How would the script know it's time to do that? What kind of access do you have...can you set a value in a file which it checks periodically, or in a database or something? Unless you simply make it reset all its variables after a set amount of time, you have to have some way of signalling to it. – ADyson Nov 27 '21 at 10:50
  • Sure, the script handles thousands websocket message, so one of them is the one to initiate that (or i can save it to the DB and check it every 1000000 go through)... that is not a problem :) I know how to unset all variables, but dont know how to "undefine" functions and uninclude includes... to have a fresh start in a new go through the loop (thank you for your time and interst @ADyson :) – Láďa Nov 27 '21 at 10:56
  • Are you saying you want to be able to redefine those functions with different code? – ADyson Nov 27 '21 at 11:04
  • yes... through new includes - is that crazy enough :) – Láďa Nov 27 '21 at 11:07
  • You might be able to do something using `eval()`, but be super careful about security – ADyson Nov 27 '21 at 12:13

1 Answers1

0

It depends on what you are trying to do with your function, I would need more info to try and help more. It seems like you could benefit from using a scheduler such as a cron job.