Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run command before exit apache/xampp

Tags:

php

apache

xampp

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.

like image 207
Moisés Avatar asked Sep 09 '16 19:09

Moisés


3 Answers

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.
like image 77
Maytham Avatar answered Oct 18 '22 21:10

Maytham


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 :)

like image 34
Halayem Anis Avatar answered Oct 18 '22 20:10

Halayem Anis


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.

like image 29
Daniel W. Avatar answered Oct 18 '22 20:10

Daniel W.