5

Is there a way to run a command/method when Apache is about to exit?

I have an application running in windows with Xampp (local use only), and want to make some kind of cleanup method when this application is ended by the user, which happens when Apache's stop button is pushed.

So any kind of event fired by Xampp or apache, or even php that I can use? And the command can be from cmd, php, whatever.

OBS: must be something before apache's closed, so check windows process list isn't an option.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
Moisés
  • 1,324
  • 15
  • 43

3 Answers3

3

IMO, I would suggest you make a batch file that do stop Apache and call your cleaning command before or after stopping command.

This way the batch file is independent from XAMPP in case of re-installation or changing environment.

The step to reach the goal:

  1. Run cmd as administrator
  2. Go to your XAMPP Apache bin folder
  3. Install Apache as service, httpd -k install
  4. Now you should be able to manually start and stop Apache by command line
httpd -k stop
httpd -k start

You can learn more commands, check the link

  1. Now make a batch file call it any thing you like, that do cleaning job and put it before or after you stop or start Apache

Example for testing cleanandstopapache.bat:

clean.bat // or what ever calling cleaning command.
pause // just to test pause
httpd -k stop // stops Apache

Notes:

  1. Normally you start and stop your XAMPP via GUI. Using service has the same effect the only different is, that you gone use command line to start and stop your Apache, that said you can put any script before stop Apache to do the job.
  2. You can either use XAMPP GUI or XAMPP services, not both.
  3. I have test it on my machine and it works.
  4. It is possible to give the service a unique customized name so you can see it in Windows Services.
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
  • 1
    Great answer, but I was looking for something that would be fired by apache or xampp, just to be sure of the cleanup. But if no such a thing is possible I'll give you the bounty in 6 days. – Moisés Sep 12 '16 at 12:37
  • Ok, and I will also meanwhile see if that is reachable and in that case I will update my answer. got my vote as well – Maytham Fahmi Sep 12 '16 at 12:40
2

You can inspire by an answer that it works fine for this stackoverflow question "Run PHP script in background on Apache start/restart(Windows Server)"

Here the link of my solution
In your case, you must replace

%APACHE_HOME%\bin\httpdVendor.exe -k runservice

by

%APACHE_HOME%\bin\httpdVendor.exe -k stopservice

I didn't test this case, and i hope that it will help you
Do not hesitate to ask me questions on your needs :)

Good luck :)

Community
  • 1
  • 1
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
1

There is a HUGE difference between application-end and Apache-end. Which one do you mean?

If you need a clean shutdown during but at the end of your Application, use a __deconstruct() method in the appripriate class.

If you need a step further in time, use a combination of ignore_user_abort() and register_shutdown_function().

If you mean Apache stopping, you should search for a script or tool that watches the process list and acts when the Apache process is not in this list.

Community
  • 1
  • 1
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • I mean Apache stopping, and I'm looking for something fired before it actually exit, somekind of apaches BeferoClosingToDoList ^^" – Moisés Sep 12 '16 at 12:32