Run PHP script in background when starting / restarting Apache (Windows Server)

I installed Apache 2.4 with PHP 5.4 on Windows Server 2008, following the instructions in this guide: Apache Installation Guide . Now Apache works as a service.

My application requires php websocket script to run in the background. I run it manually using

php myscript.php 

Question: is there a way to run a background script automatically when the system restarts (apache)?

I found the following topic , but I did not know where I can find the apache script launch for Windows.

Any help would be ambiguous.

+7
source share
9 answers

I came up with a solution :)

Create an environment variable pointing to your Apache directory
 APACHE_HOME = C:/PATH/TO_APACHE 

Rename %APACHE_HOME%\bin\httpd.exe to %APACHE_HOME%\bin\httpdVendor.exe Create a batch file and place the following code:
 php myscript.php %APACHE_HOME%\bin\httpdVendor.exe -k runservice exit 0 

Download / install the free BatToExeConverter software (next, next, ...) Open the installed converter and open your newly created batch file. Click on the Build EXE button (specify the default configuration) Save the file: %APACHE_HOME%\bin\httpd.exe Run your apache server

Tested: Windows 7, Apache 2.4, Advanced Bat to Exe Converter 2.92

+11
source

Use the built-in Windows task scheduler, which runs a .bat script that invokes curl with a specific URL.

  • Download curl from http://curl.haxx.se/download.html and extract curl.exe in any directory, but we will use c:\backgroundtasks
  • Adjust the script below for your needs:

    cd c: \

    cd c: \ backgroundtasks

    curl http: //localhost/path/to/script.php

    Exit

  • Configure the task scheduler as the main task:

    • General tab - as a system account (to start when you are not authorized on the server)
    • Triggers tab - frequency setting
    • Settings tab bottom to bottom If the task is already running... to Stop the existing instance
+2
source

The best way here is to use Windows service dependencies.

Create a php-websocket-server.cmd file with any necessary environment settings (for example, changing to a directory, setting PATH, etc.) with the last line:

 php myscript.php 

Install Windows Server Resource Kit Tools to get srvany and instsrv to create a custom service . Pay attention to the installation path that you will need in the next step.

Open the cmd shell and run:

 <path_to_resource_kit>\instsrv PHPWebSocketServer <path_to_resource_kit>\srvany.exe 

Then create a php-websocket-server.reg containing the following (update for your environment):

 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PHPWebSocketServer\Parameters] "Application"="c:\\path\\to\\php-websocket-server.cmd" 

Import it by double-clicking or regedit /s php-websocket-server.reg

Return to the cmd shell:

 sc config Apache2.4 depend= PHPWebSocketServer 

so that the Apache2.4 service * depends on your php service. Now that Apache is running, the php service will be raised first. Similarly, if you stop the php service, Apache will stop along with it.

* that indicates that the service is called "Apache2.4", but you can check it during installation.

+2
source

When launched as a service, you will not have a script running.

Perform some service implementation that allows you to run other programs as services, and then make a new service (which uses your script) an Apache service dependency. However, when you restart Apache, this will not restart the script.

One possible solution using SrvStart and the other using ServiceEx .

Maybe do not install Apache as a service, and then edit the start / restart script and use the above method to start Apache as a service (instead of using the native Apache installer).

+1
source

Create a bat file, for example, "myphp.bat" containing path/php myscript.php . Include the correct php path if this is not the path.

create a bat file, for example runmyphp.bat, containing

AT 00: 00 / each: M, T, W, Th, F "cmd / c / path / myphp.bat", again including the correct path.

Then use explorer to drag and drop runmyphp into the startup folder, so it will always start when the system starts.

Google "windows at command" or "cron" to get all the correct syntax for the "at" command, but now you can find a detailed explanation here .

+1
source

I found another answer C: \ wamp \ scripts \ wampserver.lib.php this file is run every time your wamp starts

specify the path to the file include_once("file_path"); to this file and its completion. this is the perfect solution you want

Enjoy !!!!!!!!!

+1
source

Complete all the necessary processes in a batch file and use RunAsService. With some configuration, you can make sure that your service starts before Apache.

0
source

Create Windows batch files to run Apache and a PHP script:

 net start apache || (pause && exit /b) start "PHP  01" /DC:\inetpub\scripts PHP 01.php || (pause && exit /b) 

Similarly, you can create a batch file that exits the PHP script and stops Apache:

 taskkill /FI "WINDOWTITLE eq PHP  01" net stop apache 

In the above examples:

  • Apache is the service name displayed in Services.msc; maybe instead of apache2.1 or apache2.2
  • C:\inetpub\scripts is the directory containing the script; Start command will change to this directory
  • PHP 01 is the label used in the Taskkill command to find the process
0
source

Although the Halayem Anis solution is very creative, I find it important to note that you can never be sure that the PHP script continues to run in the background. Therefore, if you decide to run your script in "Apache start", then you will probably end up resetting Apache quite often, just reload the script.

I assume that even when you come to this question, as on a regular server, you will never have to touch the Apache reset button. It starts at system startup and then starts. If so, you can simply run the php myscript.php at startup.

Given that there is no way to ensure that the script continues to work, I would use a different approach, where I check if it is running, and if not, restart it.

So, the first step is to enable you to track if the script is working. I would go for a simple approach when your myscript.php writes one byte to a file every 5 seconds or so. That way I can use the last modified time for the file to see it still works, because last modified time + 5 seconds < now == not running .

You can also save the last access time to the database every 5 seconds or so. It may be a little faster than accessing files if you have a lot of traffic.

The second part is for each request to check if the script is working. For these two works, I would use PHP.ini to add a PHP script for each request. You can do this with the auto_append_file option.

This preend script will work as follows:

 <?php $filename = 'checkonline.txt'; $cmd = "php myscript.php"; if (filemtime($filename)+5<time()) { //run in background without freezing php //based on code posted on PHP exec manual, linked below if (substr(php_uname(), 0, 7) == "Windows"){ pclose(popen("start /B ". $cmd, "r")); } else { exec($cmd . " > /dev/null &"); } } ?> 

Make sure filemtime and exec work and what you need to keep in mind. They are slightly different on Windows / * nix.

0
source

All Articles