How do I stop a PHP script?

I wrote PHP code like this:

<?php
    $i=0;
    while($i<100) {
       mail("xxxxxxx@gmail.com","SPAM","It´s ".date("d.m.o H:i:s");
       $i++;
    }
    echo "DONE!";
?>

      

I saved it in the web server directory. I am running it through my browser. How can I stop the execution of a script in time ?

Respectfully;)

+3


source to share


3 answers


If you are on a UNIX system, do the following:

ps aux | grep php


it will list all running processes which are php instances. If your script is called myscript.php then you should see php / path / to / myscript.php in this list when you run it.

Now you can kill him with the command kill -9 PID



If you are on Windows you cannot (unless you can manually open the task manager) and if you only have access to the web browser on the server and you run the script again through the web server you again cannot kill the process ... Anyway, running an infinite loop of a script without being able to kill it on command is a bad idea.

Link how to detect stopping or starting php script from background and background

+4


source


exit (0); or die (); or break; , you can use any of them.

<?php
$i=0;
while($i<100) {
   mail("xxxxxxx@gmail.com","SPAM","It´s ".date("d.m.o H:i:s");
   exit(0);
   $i++;
}
echo "DONE!";

      



? >

    output: 0   //script is stop after printing 0.

      

+1


source


For these purposes, you use exit()

or die()

. But for your example, this is not necessary as the script will abort after it runs.

0


source







All Articles