Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill background php script (shared hosting)

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.

like image 261
user793300 Avatar asked Sep 03 '11 02:09

user793300


People also ask

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.

How do you kill a PHP script?

You can use pkill -9 php or if pkill itsn't present you can use ps -efw | grep php | grep -v grep | awk '{print $2}' | xargs kill to kill all php processes.

How do I stop a PHP script in terminal?

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

Does background do in PHP?

You can put a task (such as command or script) in a background by appending a & at the end of the command line. The & operator puts command in the background and free up your terminal. The command which runs in background is called a job. You can type other command while background command is running.


2 Answers

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";
    }  
}
like image 164
symcbean Avatar answered Nov 12 '22 18:11

symcbean


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 :)

like image 43
Harsh Vardhan Ladha Avatar answered Nov 12 '22 20:11

Harsh Vardhan Ladha