我有一个系统脚本,它运行并将“ps aux | grep 实用程序”的结果通过管道传输到一个文本文件并 chowns 文本文件,以便 Web 服务可以读取该文件并在我的 Web 应用程序中显示结果。
以下是原始结果的示例:
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;
}
你们中的一些人可以为此发布优雅的解决方案吗?我正在努力学习更好的做事方式,并希望得到指导。