1

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

It's run in windows.

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

...but remember to close it afterwards if this script will be looping around this repeatedly!
0

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.

Comments

0

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.