Kill background php script (shared hosting)
Asked Answered
S

4

8

I created a script that runs in the background using the ignore_user_abort() function. However, I was foolish enough not to insert any sort of code to make the script stop and now it is sending e-mails every 30 seconds...

Is there any way to stop the script? I am in a shared hosting, so I don't have access to the command prompt, and I don't know the PID.

Saga answered 3/9, 2011 at 2:21 Comment(3)
Use system to run /bin/ps and find the PID of the offending process, then posix_kill to kill it. Maybe your shared hosting provider has a control panel of some sort that has process management tools.Decompress
If this is a singular event, you should just call the support...Hear
How did you start the script?Bandog
S
9

Is there any way to stop the script? I am in a shared hosting, so I don't have access to the command prompt, and I don't know the PID.

Then no.

But are you sure you don't have any shell access? Even via PHP? If you do, you could try....

<?php

print `ps -ef | grep php`;

...and if you can identify the process from that then....

<?php

$pid=12345; // for example.
print `kill -9 $pid`;

And even if you don't have access to run shell commands, you may be able to find the pid in /proc (on a linux system) and terminate it using the POSIX extension....

<?php

$ps=glob('/proc/[0-9]*');
foreach ($ps as $p) {
    if (is_dir($p) && is_writeable($p)) {
        print "proc= " . basename($p);
        $cmd=file_get_contents($p . '/cmdline');
        print " / " . file_get_contents($p . '/cmdline');
        if (preg_match('/(php).*(myscript.php)/',$cmd)) {
            posix_kill(basename($p), SIGKILL);
            print " xxxxx....";
            break;
        }
        print "\n";
    }  
}
Scare answered 5/9, 2011 at 8:54 Comment(0)
N
9

I came to this thread Yesterday! I by mistake had a infinite loop in a page which was not supposed to be visited and that increased my I/O to 100 and CPU usage to 100 I/O was because of some php errors and it was getting logged and log file size was increasing beyond anyone can think.

None of the above trick worked on my shared hosting.

MY SOLUTION

  1. In cPanel, go to PHP Version (except that of current)

  2. Select any PHP Version for time being.

  3. And then Apply Changes.

REASON WHY IT WORKED

The script which had infinite loop with some php errors was a process so I just needed to kill it, changing php version reinforce restart of services like php and Apache, and as restart was involved earlier processes were killed, and I was relaxed as I/O and CPU usage stabilized. Also, I fixed that bug before hand changing the php version :)

Nnw answered 5/4, 2015 at 7:54 Comment(2)
Great answer. Solution is super easy & fast, worked like a champ. Thanks!Busk
Works fine just like starting apache but in a php side. Thanks you saved my day.Gnarl
P
0

how did you deploy the script? surely you can just remove it (if that's an acceptable option). otherwise modify it and insert some logic to only allow it to send a mail once every n mins/hours/days based on the server time?

re. stopping the script from executing (or rather the system trying to execute it) how did you schedule it for execution? is it some type of gui to a crontab or something? can you not just undo what you did there (seeing as you have no access to the command line/terminal)?

rob ganly

Parasiticide answered 5/9, 2011 at 7:54 Comment(0)
H
0

Simply . Call the support, get it cancelled. Next time, don't execute something you can't control.

Hexagonal answered 22/9, 2011 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.