17

I have a simple infinite for loop looking like this:

set_time_limit (0);
for (;;)
{
    ... //Doing some stuff including to write to a file
    sleep(300);
}

It's running on my server. (Shared hosting account)

How on earth can I stop it?

Pangolin
  • 7,284
  • 8
  • 53
  • 67

5 Answers5

15

kill the process. assuming you can get access to the console via ssh and your server runs on linux:

ps -ef | grep php // to get a list of php-processes

kill [process-id] // kill the process by process-id
oezi
  • 51,017
  • 10
  • 98
  • 115
  • How do you think can I get access via SSH? Server is linux/Debian. – Pangolin Sep 23 '11 at 09:18
  • http://www.putty.org/ - just download, enter the servers ip-adress, your username and password and: voilà! – oezi Sep 23 '11 at 09:20
  • @oezi I also had a script looping on the server. I am talking about the [Amazon ec2](http://aws.amazon.com/ec2/) server. To kill this script use `ps -x` and `sudo kill [proces_id]`. Just telling... :) – Ron van der Heijden Nov 14 '12 at 15:55
  • Some shared hosting does not allow SSH, you can try http://phpshell.sourceforge.net/ you also need put php.ini to same directory as phpshell scripts, and coument out safe mode and disabled functions – Pawel Cioch Apr 14 '16 at 14:37
3

Assuming you are working with a shared hosting server, and have no access to command line or ssh or hosting support team...

your best shot would be using your website management panel to trigger php restart.

this happens when you switch php version / change php ini params such as memory limit

Yair Levy
  • 1,354
  • 16
  • 12
  • 1
    This is a good solution. Its good to point out that changing php version you might lose extensions – hkiame Mar 08 '21 at 18:47
  • 1
    also if the field has values and the table has valuable data, then taking sql backup with table structure is a clever way to restore the table after the infinite execution stops. – FerdousTheWebCoder Jun 29 '22 at 08:29
2

I logged in via SSH and tried killing the process but it didn't seem to work - possibly the incorrect process as there were quite a few there.

You can always restart apache as a last resort; that will fix it ;-)

Friendly Code
  • 1,585
  • 1
  • 25
  • 41
2

You might want to contact your hosting service and ask them to kill your script. Most likely you don't have execute-access or ssh-access. Maybe you should build a possibility to quit your program next time you create an infinite loop.

Snicksie
  • 1,987
  • 17
  • 27
0

You mentioned writing to a file.

But if you were writing to a database you can use your database tool to temporarily rename a field which would trigger an error and stop your script.

The woes of shared servers, rapid dev and typos.

BobB
  • 750
  • 1
  • 9
  • 22