5

I´ve written 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´ve saved it in the webserver directory. I start it via my browser. How can i stop the script during execution?

Best regards ;)

potashin
  • 44,205
  • 11
  • 83
  • 107
Harald Wiesinger
  • 91
  • 1
  • 1
  • 8
  • you have to try die(); – Sharma Vikram Jan 08 '15 at 07:00
  • 1
    you can't terminate server script execution from client side, the script automatic stop, see this link would be helpful you http://stackoverflow.com/questions/18075092/cant-kill-php-script – Girish Jan 08 '15 at 07:03
  • if you need to stop a script in a windows environment, there is a taskkill command that you can run at a prompt; see http://www.computerhope.com/taskkill.htm. – slevy1 Jan 08 '15 at 07:22

3 Answers3

12

If you are on UNIX system do this:

ps aux | grep php
it will list all running processes which are instances of php. If you're script is called myscript.php, then you should see php /path/to/myscript.php in this list when it's running.

Now you can kill it by command kill -9 PID

If you're running on windows you can't (unless you can pull up task manager manually) and if you only have web browser access to the server and you're running the script via the web server again you can't kill the process. Either way running an infinite loop script without the ability to kill it on command is a bad idea.

Reference how detect stop or running a php script from out and background

Community
  • 1
  • 1
Ashish Bairagi
  • 382
  • 2
  • 15
2

exit(0); or die(); or break; you can use any one.

<?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.
Priyank
  • 3,778
  • 3
  • 29
  • 48
1

You either use exit() or die() for these purposes. But for your example, this is not really necessary since the script will abort itself after it run.

Realitätsverlust
  • 3,941
  • 2
  • 22
  • 46