Question

I have a simple script, which tries to kill an already running process. I am using posix_kill for the same. The script runs fine, if I run it from command shell, but doesn't work when I run it from the browser.

<?php

    $taskid  = 33;
    killProcess($taskid);

    function killProcess($taskid) {
    $ppid = getPID($taskid);
    echo "process id -- $ppid\n";
    $pids = preg_split('/\s+/', `ps -o pid --no-heading --ppid $ppid`);
    var_dump($pids);
    foreach($pids as $pid) {
            if(is_numeric($pid)) {
            echo "Killing $pid\n";
                posix_kill($pid, 15);
            }
    }

    }
?>

I always get the following output, which means that the script is executing fine, but the process does not get killed: process id -- 4632 array (size=3) 0 => string '' (length=0) 1 => string '4633' (length=4) 2 => string '' (length=0) Killing 4633

I checked out the apache "error.log" file, and found that for each run that I do from browser, and additional entry gets added as follows: [Sat Jun 23 12:24:55 2012] [error] [client 127.0.0.1] File does not exist: /var/www/favicon.ico

Unfortunately this error does not give me much information about what's going wrong. I checked out the php.ini settings, and there doesn't seem to be anything wrong (as per whatever limited understanding I have). I do not see posix_kill in the "disabled_functions" list etc or any other setting that should prevent me from executing process.

Would sincerely appreciate some help !!

Thanks, Kapil

Was it helpful?

Solution

The error message has nothing to do with your problem. It's just the browser asking for your site's icon, which you haven't configured.

Now, about the actual issue: the apache user likely doesn't have permissions to kill your process. If the process was started under a different user account, you can't kill it from within the web server. Imagine a multiuser system where any local user could log in and kill everyone else's processes - that isn't the Unix permissions model...

You might want to use a limited sudo with exec, or otherwise assume permissions before sending the signal. Or you could have the process listen for the web server to do something, and then kill itself. Or, better yet, don't use a web-serving language like PHP for systems-administration type tasks: use cron or a more appropriate tool.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top