Restarting Windows Services in a PHP Script

Recently we needed a PHP script to restart a few services on a Windows Server. You can activate an extension for PHP called “Win32Service” that provides the necessary functions in PHP (see PHP manual).

Getting the necessary Library

You need the “php_win32sevices.dll” file that fits your PHP installation. Go to <a href=”http://www.php.net/releases/”>http://www.php.net/releases/</a&gt; and download the “Collection of PECL modules for PHP” that fits your release. Unzip the file and place in your extensions folder (“ext”).

See also: PHP manual

Active the library in your PHP configuration

Add this line to your PHP .INI file
extension=php_win32service.dll
and restart the Apache service.

Create a PHP Script

Here is the PHP Script that we use. It performs the service restart in 4 steps:

  • Send the stop command to the service
  • Wait until service is stopped
  • Send the start command to the service
  • Wait until service is started
  • Show an error if service does not start

<html><body>
<h1>Restart all Services</h1>
<?php
/* PHP Script to restart a Windows service */
/* (c) 2008 Paessler AG (www.paessler.com) written by Dirk Paessler */
function Status($name)
{
 $status=win32_query_service_status($name);
 echo "Service state is ";
 if ($status["CurrentState"]==1) {echo "Stopped";}
 else
 if ($status["CurrentState"]==2) {echo "Starting";}
 else
 if ($status["CurrentState"]==3) {echo "Stopping";}
 else
 if ($status["CurrentState"]==4) {echo "RUNNING";}
 echo "<br>";
 return $status["CurrentState"];
/* SERVICE_STOPPED    = $1;
 SERVICE_START_PENDING       = $2;
 SERVICE_STOP_PENDING        = $3;
 SERVICE_RUNNING             = $4;
 SERVICE_CONTINUE_PENDING    = $5;
 SERVICE_PAUSE_PENDING       = $6;
 SERVICE_PAUSED              = $7; */
}
function RestartOne($name)
{
 echo "<h2>Restarting Service '".$name."'</h2>";
 Status($name);
 echo "Sending Stop Command...";
 flush();
 $result=win32_stop_service($name);
 echo " (Result: ".$result.")<br>";
 Status($name);
 // Sleeping and waiting for service to stop for maximum 50 seconds
 $count=0;
    do
    {
     flush();
   sleep(1);
   $count=$count+1;
   if ($count==10) {win32_stop_service($name);} //reissue stop command after 10 seconds
   if ($count==20) {win32_stop_service($name);} //reissue stop command after 20 seconds
   $laststate=Status($name);
  }
 while (($laststate!=1) and ($count<50));
echo "Sending Start Command... ";
 flush();
 $result=win32_start_service($name);
 echo "(Result: ".$result.")<br>";
 flush();
 // Sleeping and waiting for service to start for maximum 50 seconds
 $count=0;
    do
    {
     flush();
   sleep(1);
   $count=$count+1;
   if ($count==10) {win32_start_service($name);} //reissue stop command after 10 seconds
   if ($count==20) {win32_start_service($name);} //reissue stop command after 20 seconds
   $laststate=Status($name);
  }
 while (($laststate!=4) and ($count<50));
 if ($laststate!=4)
 {
  Echo "ERROR: Service '".$name."' did not start, sending just one more start command";
  $result=win32_start_service($name); //give it one last try...
  }
}
RestartOne("myservicename");
Echo "<h3>Done. All OK</h3>";
?>

Advertisements

4 Responses to “Restarting Windows Services in a PHP Script”

  1. Abhishek Dixit Says:

    Hi Team,
    Please tell me how to start windows services via running scripts.Some of the services i have mentioned below:
    Print Spooler
    DHCP
    DNS
    TCP/IP

  2. Peter DeMarco Says:

    I’m trying to get this to work. Complied in the module, shows as enabled in phpinfo();. When I try to run win32_query_service_status() ( Trying with multiple service names ) and try to print_r() the array that’s supposed to be there, all I get is the integer 51. Any ideas? I can’t find any other documentation of this so you’re my best bet.

  3. Martin Says:

    open a cmd.exe and type

    net helpmsg 51

  4. Dirk Says:

    Ok, that would mean #51 is:

    “Windows cannot find the network path. Verify that the network path is correct and the destination computer is not busy or turned off. If Windows still cannot find the network path, contact your network administrator.”


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: