0

我无法使用 kill 命令杀死在 cakePHP 中运行的作业。我的作业控制器的索引视图中的表格中显示了一个进程列表,每个正在运行的进程都有一个链接,您可以单击该链接将其终止。

我有这样的事情:

//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";
?>

然后在 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);
        }
    }

然后在 myApp/app/killProcess.sh 中,我有:

kill -15 $1

但是当我点击杀死一个工作时,它并没有停止。虽然如果我从命令行手动运行命令,它确实会杀死它。根据我的阅读,我认为这可能与没有 sudo 权限有关,现在基于我尝试更改代码

myApp/app/killProcess.sh 到:

sudo kill -15 $1

然后重试它,但这也不起作用。

我怎样才能让它工作?任何帮助将非常感激。

4

0 回答 0