1

I am using the linux command nohup php filename.php & to create a background php process. The problem here is that, if there are any changes in the files included in filename.php or even the file itself, I have to kill the current process and start it again. Is there any way to prevent this, so that no restart is required on code update, because I do not want any downtime(because of killing and restarting the process) in my application. If there is no other way around this, then would anyone suggest an alternate approach?

EDIT 1

I am using ZeroMQ for communication between the main process, and this background process. The background process binds to a particular TCP port, say 555. Now, if I try to start a new process, and try to bind it to the same port, it gives an error, as the port is already in use. And thus, I need to kill the process before starting a new one.

EDIT 2

Please check my other question for in-dept explanation of my complete problem with sample code.

Community
  • 1
  • 1
Parthapratim Neog
  • 4,352
  • 6
  • 43
  • 79
  • How about, start a new process with the amended code before killing the old process? Would that work – RiggsFolly May 16 '16 at 10:24
  • @RiggsFolly I am using ZeroMQ for communication between the main process, and this background process. The background process binds to a particular TCP port, say 555. Now, if I try to start a new process, and try to bind it to the same port, it gives an error, as the port is already in use. And thus, I need to kill the process before starting a new one. – Parthapratim Neog May 16 '16 at 10:26
  • The PHP script is loaded once when execution starts. There doesn't seem to be any way around this. If your background process is a constant loop you could separate out the code in the loop into a different file and call that file via `exec` or `shell_exec` that way you're forcing the script to reload on each iteration. – apokryfos May 16 '16 at 10:32
  • @apokryfos Please check [this](http://stackoverflow.com/questions/37189154/is-this-the-right-way-to-use-a-messaging-queue) question of mine, which is a more in-depth version of the same problem. I think u will be able to help me out on this. – Parthapratim Neog May 16 '16 at 10:33
  • Would it work if your code looped around the bind process until it got a connection. Then the new process would pick up as soon as the old process was killed – RiggsFolly May 16 '16 at 10:39
  • @RiggsFolly I am not sure of that, but I will definitely give that a try. Can you please check [this](http://stackoverflow.com/questions/37189154/is-this-the-right-way-to-use-a-messaging-queue) question of mine in the meantime which has a more in-dept description of my problem with sample code. I had to post a simpler overall question as this, because I was not getting any response in the other question. Would appreciate it if you'd give it a look. – Parthapratim Neog May 16 '16 at 10:42

1 Answers1

1

You can not update your code while the script is running. PHP does not even do this when it is run with apache. However PHP does check before a new request is started whether it should reload the code.

If performance is not a big issue. You could do something similar yourself. Have a main script (that does not need to change much) listen on the queue. And start a second script using proc_open for instance to run the second script and serialize the data over the pipes. Now when ever the second script is used it will be reloaded for every request.

Dave Kok
  • 892
  • 9
  • 19