0

I am having trouble being able to kill a job running in cakePHP, using the kill command. I have a list of processes shown in a table in the index view of my Jobs Controller, with a link for each process that is running, that you can click to kill it.

I have something like this:

//myApp/app/Views/Jobs/index.php:

<?php
    echo $this->Form->create('Job', array('action' => 'actOnJob', 'onsubmit' => 'return confirm("are you sure?");'));

    echo "<table>\n";

    /* irrelevant code excluded */

    $nonOrphanPids = array();

    foreach ($jobs as $job) {

        /* irrelevant code excluded */

        if ($job['Job']['is_running'] == 1 && $job['Job']['pid'] != null) {
            echo "    <td>";

            //KEY PART OF CODE FOR THIS QUESTION, this is a link that attempts to abort the job selected
            echo $this->Html->link('Abort Job', array('action' => 'actOnJob', $id, 'abort'), array('confirm' => __('Are you sure?')));
            echo "</td>";
            echo "    <td>";
            echo $this->Form->input('',
                array(
                    'type' => 'checkbox',
                    'name' => "cbAbort$id",
                    'id' => "cbAbort$id",
                    'default' => isset($_REQUEST["cbAbort$id"]) ? $_REQUEST["cbAbort$id"] : 0
                )
            );
        } else {
            echo "    <td colspan='2'>";
        }
        echo "    </td>";

        /* irrelevant code excluded */
    }

    foreach ($orphanJobsDetails as $ophanJobDetails) {
        if (!in_array($ophanJobDetails['PID'], $nonOrphanPids) && 
            strpos($ophanJobDetails['COMMAND'], 'grep insertVehicleData.py') === false)
        {    

            /* irrelevant code excluded */

            //KEY PART OF CODE FOR THIS QUESTION, this is a link that attempts to abort the job selected
            echo $this->Html->link('Abort Job', array('action' => 'actOnJob', $id, 'abort'), array('confirm' => __('Are you sure?')));

            echo "  <td>";
            echo $this->Form->input(
                '', array(
                    'type' => 'checkbox', 
                    'name' => "cbAbort$id",
                    'id' => "cbAbort$id",
                    'default' => isset($_REQUEST["cbAbort$id"]) ? $_REQUEST["cbAbort$id"] : 0
                )
            );
            echo "  </td>";

            /* irrelevant code excluded */
        }
    }

    echo "</table>\n";

    echo "</div>\n";
?>

Then in myApp/app/Controller/JobsController.php:

class JobsController extends AppController {

    public function actOnJob($id = 0, $action = '') {

        $this->autoRender = false;

        /*irrelevant code excluded*/

        else if ($action == 'abort') {
            $this->abort($id);
        }

        /*irrelevant code excluded*/

        $this->redirect(env('HTTP_REFERER'));
    }

    public function abort($id) {
        if (strpos($id, '_') === false) {
            $job = $this->Job->find('first', array('conditions' => array('id' => $id))); //, 'fields' => array('pid', 'is_running')));

            if ($job['Job']['is_running'] == 1) {
                $cmd = env('DOCUMENT_ROOT') . '/app/killprocess.sh ' . $job['Job']['pid'];

                //originally tried the following 2 lines, one a time, before trying `shell_exec($cmd)`;
                //posix_kill($job['Job']['pid'], 9);
                //posix_kill($job['Job']['pid'], 15);
                shell_exec($cmd);

                $this->Job->id = $id;
                $this->Job->save(array('Job' => array('is_running' => 0, 'pid' => null)));
                $this->loadModel('JobRunnings');
                $this->JobRunnings->save(array('JobRunnings' => array('is_running' => 0, 'id' => 1)));
            }
        } else {
            $pid = intval(substr($id, 1));
            $cmd = env('DOCUMENT_ROOT') . '/app/killprocess.sh ' . $pid;
            $this->Session->setFlash($cmd);
            shell_exec($cmd);
        }
    }

Then in myApp/app/killProcess.sh, I have:

kill -15 $1

But when I click to kill a job, it doesn't stop. Although if I run the command manually from the command line, it does kill it. From what I read, I think it may have something to do with not having sudo permissions, now based on that I tried changing the code in

myApp/app/killProcess.sh to:

sudo kill -15 $1

And then retrying it, but that does not working either.

How do I get this to work? Any help would be much appreciated.

user1296259
  • 521
  • 1
  • 13
  • 33
  • Usually php is run with a special user that does not have admin rights. Sudo needs to enter the password. If you do not enter the password it does not work. I will try to find the variant of sudo command with password written in it. It may work for you. – keiv.fly Jun 09 '14 at 15:24
  • Try this: `$echo | sudo -S ` – keiv.fly Jun 09 '14 at 15:31
  • This is all permission related. If you rewrite to obtain and check the result (e.g. use [exec](http://www.php.net//manual/en/function.exec.php) and all 3 args) - the shell_exec call will be failing, with an error message. What user is running the job to be killed (that's not a normal thing to be doing.. do you really need to do that? why?), and what user is the webserver (apache) running as? sudo isn't going to work directly and *shouldn't* be part of a solution. – AD7six Jun 09 '14 at 19:51

0 Answers0