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

Firefox to get massive JavaScript performance boost

Mozilla is leveraging an impressive new optimization technique to bring a big performance boost to the Firefox JavaScript engine (and we are talking about 20 times more speed and more). The code was merged today but is not yet ready to be enabled by default in the nightly builds.

Read more: Firefox to get massive JavaScript performance boost

This page explains how to enable the new “compiler” in Firefox 3.1 latest Nightly Build.

But don’t expect too much from it yet for most standard Javascript/jQuery projects in Web browsers:

Right now there isn’t any tracing being done into DOM methods (only across pure-JavaScript objects) – but that is something that will be rectified. Being able to trace through a DOM method would successfully speed up, not only, math and object-intensive applications (as it does now) but also regular DOM manipulation and property access.

A real on-line photoshop express contender!

Try out the new pixlr image editor. Impressive flash work. Look and feel like a real app and the power to adjust, edit and filter for all your needs.

read more | digg story

JSLint, The JavaScript Verifier

JSLint, The JavaScript Verifier  is a JavaScript program that looks for problems in JavaScript programs. JSLint takes a JavaScript source and scans it. If it finds a problem, it returns a message describing the problem and an approximate location within the source. The problem is not necessarily a syntax error, although it often is. JSLint looks at some style conventions as well as structural problems. It does not prove that your program is correct. It just provides another set of eyes to help spot problems.

Folder c:\ProgrammData\microsoft\Windows\WER\ReportQueue eats Vista C: Drive Space

Windows Vista ‘WER’ Report Queue Folder can become huge on Vista computers. “WER” is Windows Error Reporting and took more than 1 GB of disk space on my PC recently. I discovered that by accident when reviewing the list of fragmented files and found many 20 MB files with 300 fragments or more.

What to do about it: You can clear that through Vista’s “Disk Cleanup” (right click the drive icon in Explorer and select “Properties” and then “Tools”. This gave me back 15 GB of diskspace on my system drive.

Definitely worth checking from time to time if you have apps that crash a lot and use Windows Vista. Software developers and testers will likely have this situation!

WER can be turned off in the “Problem Reports and Solutions” applet in Control Panel.

Background Info on Microsoft technet: http://technet2.microsoft.com/WindowsVista/en/library/03a97dba-6b24-42c7-81f0-fb751f6e839d1033.mspx?mfr=true

Posted in Links. 4 Comments »

Two New Free Disk Defraggers for Vista

Defraggler

Defraggler from Piriform, the company that brought CCleaner and Recuva, now there is a free file defragmentation tool… Defraggler! It differs from other defrag tools on the market, by enabling you to quickly and simply defrag the files you want to, without having to process the whole drive. Simply run it, select the file and defragment in seconds. No more struggling with the Windows defragmentation tool!

Note: As of Aug 4th 2008 the download is marked “RC2” (Release Candidate 2)

Auslogik Disk Defrag

Auslogic Disk Defrag has a nice interface and seems to do the job well, too.

PS: I mentioned ultradefrag here already

Tab Scope :: Firefox Add-ons

Tab Scope shows a popup on tabs and enables you to preview and navigate tab contents through popup. Major features are:

* Real-time preview of tab contents
* Navigate (Back/Forward/Reload/Stop) through popup
* Scroll pages or frames in preview with mouse wheel
* Click links or buttons directly in preview

For more info, please see http://www.xuldev.org/tabscope/