How to get list of PHP processes running on server with PHP

前端 未结 2 585
后悔当初
后悔当初 2021-01-06 08:44

I have a cronjob that runs a PHP file that runs a DAEMON written in PHP, but I only want to run the DAEMON if no other instances of it are running, how can I get a list of P

2条回答
  •  庸人自扰
    2021-01-06 08:54

    To get the list of PHP processes see this question:

    How to get list of running php scripts using PHP exec()?

    Another option is that you can acquire a lock of the file and then check it before running: for example:

    $thisfilepath = $_SERVER['SCRIPT_FILENAME'];
    $thisfilepath = fopen($thisfilepath,'r');
    if (!flock($thisfilepath,LOCK_EX | LOCK_NB))
    {
      customlogfunctionandemail("File is Locked");
      exit();
    }
    elseif(flock($thisfilepath,LOCK_EX | LOCK_NB)) // Acquire Lock
    {
      // Write your code
    
    
     // Unlock before finish
     flock($thisfilepath,LOCK_UN);  // Unlock the file and Exit
     customlogfunctionandemail("Process completed. File is unlocked");
     exit();
    }
    

    Basically in the above example you are first checking if the file is locked or not and if it is not locked (means process is completed) you can acquire lock and begin your code.

    Thanks

提交回复
热议问题