0

I have a PHP script which will run 24/7 on a VPS with an infinite loop, but problem is that how can I break the infinite loop in php script when desired in order so that the script exists properly?

I know I can kill the process, but if I kill then script will stop immediately and my loop size is very big and if at time of killing the process the execution of is somewhere in middle of loop then the rest half of loop would not be executed and it will create bugs. Also I have some code after loop so if process gets killed then that code would not be executed.

So my question is that how to break a infinite loop of a PHP script running in background when desired without killing the process?

2
  • 2
    If you have two questions, you should create two separate questions then. Commented Feb 8, 2015 at 14:15
  • Since your process is an HTTP scraper, compiling your script into a binary is probably not going to make much difference - it is still network bound. Commented Feb 8, 2015 at 14:40

3 Answers 3

2

Instead of:

while(true) {

Do:

while(!file_exists("KILL")) {

Then, when you want to kill the script, simply create the KILL file. You could make it easier for yourself by having a kill.php script that just creates that file for you, so you don't have to remember exactly what to do.

Personally I find PHP scripts easier to work with over EXEs for console apps, but that's just opinion.

Sign up to request clarification or add additional context in comments.

2 Comments

That helped me alot! Thanks Q1 is answered. About Q2 which is better performance wise?
PHP is interpreted, EXE is compiled, therefore EXE will be better for performance. However it's harder to work with, so up to you.
0

Q1. Windows Task Scheduler.. don't insert any loop, just insert CGI directory,then create time.

Q2. PHP would be alot better if you ask me.

although, It's not about Performance.

Comments

0

Sounds like you're coding a server, take a look at this, hope it helps.

<?php
class ServerManager
{
    private $serverState;

    public function process()
    {
        if(!$this->getServerState())
        {
            return;
        }

        $this->processNewConnections();
        $this->processExistingConnections();
    }

    public function setServerState($state)
    {
        $this->serverState = $state;
    }
    public function getServerState()
    {
        return $this->serverState;
    }

    private function processNewConnections()
    {
        ...
    }

    private function processExistingConnections()
    {
        ...
        if(connected client has requested that the server should stop)
        {
            $this->setServerState(false);
        }
        ...
    }
}

3 Comments

Hey guys, I found Clippy!
Ah nope I am not coding server, its a real time chat scraper that scapres chat out of a protected webpage every 5 seconds and performs various tasks with scraped data.
Okay, well even so, you probably want something similar to what I have demonstrated in terms of stopping the infinite loop and not continuing processing.

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.