1. 程式人生 > 實用技巧 >判斷指令碼是否在執行

判斷指令碼是否在執行

 /**
     * 判斷指令碼是否在執行
     * @param $script_name
     * @return array
     */
    public function isRunning($script_name)
    {
        try {
            exec('sudo ps -aux | grep ' . $script_name, $output, $return_var);
            if ($return_var == 0) {
                foreach ($output as $key => $row) {
                    $temp_arr 
= []; foreach (explode(' ', $row) as $column) { if (!empty($column)) { $temp_arr[] = $column; } } if (in_array($script_name, $temp_arr)) { return
['code' => StatusCode::SUCCESS, 'data' => [], 'msg' => '指令碼已經在執行']; } else { return ['code' => StatusCode::FAILED, 'data' => [], 'msg' => '指令碼沒有在執行']; } } } return ['code' => StatusCode::INNER_ERROR, '
data' => [], 'msg' => 'exec執行指令碼失敗, 返回狀態return_var: ' . $return_var . '; $output:' . json_encode($output)]; } catch (\Exception $e) { return ['code' => StatusCode::INNER_ERROR, 'data' => [], 'msg' => '內部錯誤,請聯絡管理員,' . $e->getMessage()]; } }