Home>

Hello everyone, I've created a script that removes PHP processes that have been running for more than n hours. I think this is bad code and would like to hear your opinion on how to improve this code, for example where more logical commands can be used. I will add this script to Crontab. It seems to me that it will be removed and it will not work.

How can I make it ignore itself?

<?php
    if(!isset($argc) || ​​$argc != 2 || !is_numeric($argv[1])){
        echo "Error. Syntax: \e[32mphp processKill.php [valoarea in secunde]\e[39m." . PHP_EOL;
        exit;
    }elseif (isset($argc) &
&
 $argc=== 2){
        $filename= __DIR__ . "/ResultProcessKill/" . date("Y-m-d_H-i-s") . ".txt";
        $process= shell_exec("ps -eo pid,lstart,cmd | grep php");
        $arrayProcess= explode(PHP_EOL, $process);
        $arrayProcessPart= [];
        $currentDate= date("Y-m-d H:i:s");
        $month= ["Jan"=> 1,"Feb"=> 2,"Mar"=> 3,"Apr"=> 4,"May"=> 5,"Jun"=> 6,"Aug"=> 7,"Aug"=> 8,"Sep"=> 9,"Oct"=> 10,"Nov"=> 11,"Dec"=> 12];
        $deleteTime= $argv[1];
        for($i= 0; $i < count($arrayProcess) -1; $i++){
            $arrayProcess[$i]= trim($arrayProcess[$i]);
            $arrayProcessPart[$i]= preg_split(" /[\s,]+/", $arrayProcess[$i]);
            $processDate[$i]= $arrayProcessPart[$i][5] . "-" . $month[$arrayProcessPart[$i][2]] . "-" . $arrayProcessPart[$i][3] . " " . $arrayProcessPart[$i][4];
            $diffDate= strtotime($currentDate) -strtotime($processDate[$i]);
            if($diffDate < $deleteTime){
                $deleteTextProcess= "kill" . $arrayProcessPart[$i][0];
                shell_exec($deleteTextProcess);
                $result= "\e[31m";
                $addTex= "Removed -";
            }else{
                $result= "\e[39m";
                $addText= "";
            }
            //$text= "The script is running : " . $diffDate . "sec." . PHP_EOL . $arrayProcess[$i] . PHP_EOL;
            $text= $result . $arrayProcess[$i] . PHP_EOL;
            echo $text;
            file_put_contents($filename, $addTex . $text, FILE_APPEND);
        }
    }
?>

How to ask a code review question

Dmitry2022-02-14 08:31:13

The getmypid() function returns the process's own pid, so ignore it. But it is not clear why he should ignore himself. It is not constantly running, which means it cannot get under deletion in any way.

Mike2022-02-14 08:36:02

@Mike oops, that's right, stupid. It will be executed once and will fail. What about the code if there are suggestions for improvement?)

Проста Miha2022-02-14 08:39:05
  • Answer # 1

    What about the code, if there are any suggestions for improvement?)

    Well, you asked for it yourself... :)

    if( !isset($argv) || !is_array($argv) || count( $argv ) < 1 || !is_numeric($argv[1]) || intval($argv[1]) < one ) {
            echo "Error. Syntax: \e[32mphp processKill.php [seconds]\e[39m." . PHP_EOL;
            exit;
        }
        $deleteTime= intval($argv[1]);
        $process= shell_exec("ps -eo pid,lstart,cmd | grep php");
        $arrayProcess= explode(PHP_EOL, $process);
        if ( is_array( $arrayProcess ) &
    &
     count( $arrayProcess ) > 0 ) {
           foreach( $arrayProcess as $ap ) {
               $ap= trim($ap);
               $apPart= preg_split(" /[\s,]+/", $ap);
               if ( is_array( $apPart ) &
    &
     count( $apPart ) >= 6 ) {
                   $diffDate= time() -strtotime( $apPart[5]."-".$apPart[2]]."-".$apPart[3]." " .$apPart[4] );
                   $result= "\e[39m";
                   $addText= "";
                   if( $diffDate < $deleteTime ){
                       shell_exec( "kill " . $apPart[0] );
                       $result= "\e[31m";
                       $addTex= "Removed -";
                   }
                   $text= $result . $ap . PHP_EOL;
                   echo $text;
                   file_put_contents( __DIR__ . "/ResultProcessKill/" . date("Y-m-d_H-i-s") . ".txt", $addTex . $text, FILE_APPEND);
               }
           }
        }