Running a php script via ajax, but only if it is not already running

后端 未结 6 1586
轮回少年
轮回少年 2021-02-13 21:28

My intention is this.

My client.html calls a php script check.php via ajax. I want check.php to check if another script task.php is already being run. If it is, I do not

6条回答
  •  有刺的猬
    2021-02-13 21:55

    Lets make the whole process from B to D simple

    Step B-D:

    $rslt =array(); // output from first exec
    $output = array(); // output of task.php execution
    
    //Check if any process by the name 'task.php' is running
    exec("ps -auxf | grep 'task.php' | grep -v 'grep'",$rslt);
    
    if(count($rslt)==0) // if none,
      exec('php task.php',$output); // run the task,
    

    Explanation:

    ps -auxf        --> gets all running processes with details 
    grep 'task.php' --> filter the process by 'task.php' keyword
    grep -v 'grep'  --> filters the grep process out
    

    NB:

    1. Its also advisable to put the same check in task.php file.

    2. If task.php is executed directly via httpd (webserver), it will only be displayed as a httpd process and cannot be identified by 'ps' command

    3. It wouldn't work under load-balanced environment. [Edited: 17Jul17]

提交回复
热议问题