tags:

views:

60

answers:

3

It should test if a specific port is open on localhost,if not,reboot.

It's run in windows.

A: 

You can write a PHP extension to do that. Extension should use Windows API to reboot the machine because the socket checking part can be done straight in PHP. Here is a question on how to write extensions.

InitiateSystemShutdown is the Win32 API function you can call to do the actual rebooting.

Pablo Santa Cruz
A: 

Use sockets you need to open a TCP connection (socket) to the localhost with that specific port. If the connection is establish, it means the port is open, otherwise (if timed-out or rejected), then the port is closed.

that's only for TCP ports

here's a sample code

For the 'reboot' part, use exec('shutdown -r');

Aziz
+1  A: 

This should do it, tested on Windows 7 and working. Should work on all NT flavours:

function testPort($port, $timeout = 5) {
    if(!fsockopen('127.0.0.1', $port, $errno, $errstr, $timeout)) {
        exec("shutdown.exe /r");
    }
}

testPort(8080);
Tatu Ulmanen
It should work (if he can `exec()`).
Alix Axel
...but remember to close it afterwards if this script will be looping around this repeatedly!
symcbean
.....but wouldn't it be less obtrusive to restart the relevant service?
symcbean