出于监控目的,我想将当前进程的打开文件量转发给我们的监控工具.在shell中,可以执行以下命令来获取所需的信息:ls /proc/PROCES_ID/fd | wc -l
其中PROCES_ID是当前的进程ID.有没有一种方法可以在PHP中以本机方式获取这些信息?
出于监控目的,我想将当前进程的打开文件量转发给我们的监控工具.在shell中,可以执行以下命令来获取所需的信息:ls /proc/PROCES_ID/fd | wc -l
其中PROCES_ID是当前的进程ID.有没有一种方法可以在PHP中以本机方式获取这些信息?
要从php脚本中运行任何shell命令并获得输出:
exec(string $command, array &$output = null, int &$result_code = null): string|false
命令:
$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);
但正如 comments 中提到的,如果可行的话,最好使用bash脚本而不是php.