Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop PHP Script? [duplicate]

I´ve written PHP-Code like this:

<?php
    $i=0;
    while($i<100) {
       mail("[email protected]","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 ;)

like image 913
Harald Wiesinger Avatar asked Jan 08 '15 06:01

Harald Wiesinger


People also ask

How do I stop a PHP script?

The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit() function is called.

How do I stop a PHP script from running in the background?

If you started it in background use ps aux | grep time. php to get PID. Then just kill PID . If process started in foreground, use to interrupt it.

Can a PHP script run forever?

Whenever a PHP application rebuilds MySQL indexes, the process may run for a long time. Generally, allowing a PHP script to run forever is not desirable.

Which function kills the execution of the script immediately in PHP?

I am aware of exit but it clearly states: Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.


1 Answers

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

like image 174
Ashish Bairagi Avatar answered Oct 22 '22 14:10

Ashish Bairagi