phpphp-8

Get amount of open files of current process


For monitoring purposes, I want to forward the amount of open files of the current process to our monitoring tool. In shell, the following command can be executed to get the desired information: ls /proc/PROCES_ID/fd | wc -l where PROCES_ID is the current process id. Is there a way to get this information natively in PHP?


Solution

  • To run any shell commands from within php script and get the output:


    From PHP manual on exec() command

    exec(string $command, array &$output = null, int &$result_code = null): string|false
    

    For your command:

    $output = []; // this array will hold whatever the output from the bash command
    $result_code = null; // this will give you the result code returned, if needed
    $command = '/proc/PROCES_ID/fd | wc -l'; // command to be run in the bash
    exec($command, $output, $result_code);
    
    //now you can look into $output array variable for the values returned from the above command
    print_r($output);
    

    But as mentioned in the comment, using bash script over php should be preferred if feasible.