dongyun8075 2015-11-12 14:42 采纳率: 100%
浏览 22

使用PHP,检查脚本是否正在运行并同时运行其他功能

I've PHP function which starts a shell script in my Linux system when a user presses the start button.

public function start($id)
{
    $test=Test::find($id);
    $newFile=storage_path('test/script/'.$test->random_string.'.jmx');
    $command = "jmeter -n -t ".$newFile.' -l '.storage_path('test/log/'.$test->random_string).'.log';
    $background= "  > /dev/null 2>&1 &";

    // To allow running test in background
    ob_end_clean();
    header("Connection: close");
    ignore_user_abort(); // optional
    ob_start();
    $size = ob_get_length();
    header("Content-Length: $size");
    ob_end_flush(); // Strange behaviour, will not work
    flush();            // Unless both are called !
    exec($command. $background,$arr);
    // Do processing here
    sleep(30);
}

I want another PHP function to know when the script ends and show the response to the user.

public function end($id)
{
    $test = Test::find($id);
    $command="ps aux | grep $test->random_string | grep -v grep | awk '{ print $2 }'";
    exec($command, $arr);
    while($arr)
    {
        $arr=null;
        sleep(10);
        exec($command, $arr);
    }
    $test->test_running = 0;
    $test->save();
    return response()->json(['msg'=>trans('test.end')]);
}

I also want to give my user, option to stop script in middle and show the result of half script.

public function stop($id)
{
    $test=Test::find($id);
    $arr=array();
    $background= "  > /dev/null 2>&1 &";
    exec("ps aux | grep $test->random_string | grep -v grep | awk '{ print $2 }'", $arr);
    shell_exec('kill '. $arr[1] . $background);
    $test->test_running=0;
    $test->save();
    return response()->json(['msg'=>trans('test.stop')]);
}

Problem is while end function is running it doesn't allow stop function(or any other function) to do any processing. Javascript shows response as pending in my browser and sending another request for stop also remain pending until end request is not completed.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算