我有一个系统脚本,它运行并将“ps aux | grep工具”的结果传输到文本文件并传递文本文件,以便Web服务可以读取文件并在我的Web应用程序中显示结果。php来解析ps aux | grep ...结果
这里是原始结果的一个例子:
user 12052 0.2 0.1 137184 13056 ? Ss 10:00 0:00 php /home/user/public_html/utilities/runProcFile.php cust1 cron
user 12054 0.2 0.1 137184 13064 ? Ss 10:00 0:00 php /home/user/public_html/utilities/runProcFile.php cust3 cron
user 12055 0.6 0.1 137844 14220 ? Ss 10:00 0:00 php /home/user/public_html/utilities/runProcFile.php cust4 cron
user 12057 0.2 0.1 137184 13052 ? Ss 10:00 0:00 php /home/user/public_html/utilities/runProcFile.php cust89 cron
user 12058 0.2 0.1 137184 13052 ? Ss 10:00 0:00 php /home/user/public_html/utilities/runProcFile.php cust435 cron
user 12059 0.3 0.1 135112 13000 ? Ss 10:00 0:00 php /home/user/public_html/utilities/runProcFile.php cust16 cron
root 12068 0.0 0.0 106088 1164 pts/1 S+ 10:00 0:00 sh -c ps aux | grep utilities > /home/user/public_html/logs/dashboard/currentlyPosting.txt
root 12070 0.0 0.0 103240 828 pts/1 R+ 10:00 0:00 grep utilities
至于我的PHP脚本解析这个文本文件,我只需要提取以下(只是一个例子):
cust1
cust3
cust4
cust89
cust435
cust16
我有尝试了许多不同的笨拙方式,似乎没有什么效果。我在下面列出的方法是有效的,但有时也会因为一行中“空格”的数量在更改时爆炸而占用垃圾。
public function showProcesses() {
$lines = file(DIR_LOGGER_ROOT . "dashboard/currentlyPosting.txt");
$results = array();
$i = 0;
foreach($lines as $line) {
if (preg_match("/php/i", $line)) {
$userProcess = explode(" ", $line);
if($userProcess[29] != "0:00" && strlen($userProcess[29]) < 20) {
$results[$i] = $userProcess[29];
$i++;
}
}
}
return $results;
}
难道你们中的一些人会为此发布优雅的解决方案吗?我正在努力学习更好的做事方式,并希望得到指导。
如果你正在写一个命令行应用程序要做到这一点,我建议使用更适合字符串处理和正则表达式如Perl语言。你甚至可能甚至可以在CPAN上找到一些东西直接访问进程表,例如http://search.cpan.org/dist/Proc-ProcessTable/ –
,数据将从系统脚本中收集并写入文本文件。它使我的Web应用程序可访问文本文件,而无需使用我的Web应用程序中的exec和root。但解析结果是为了Web应用程序本身,所以PHP是有道理的。 –