2013-01-02 20 views
5

Bir metin dosyasına "ps aux | grep yardımcı programları" sonuçlarını çalıştıran ve bu dosyalara boru ekleyen ve web hizmetinin dosyayı okuyabildiği ve sonuçları web uygulamasında görüntüleyebildiği bir sistem komut dosyasına sahibim. İşte php ps aux | grep ... sonuçlar

ham sonuçların bir örnektir:

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 

benim php komut dosyası bu textfile ayrıştırır olarak, sadece aşağıdaki (sadece bir örnek) ayıklamak gerekir:

cust1 
cust3 
cust4 
cust89 
cust435 
cust16 

Ben Bir dizi farklı sakarca yolu denedi ve hiçbir şey iyi gibi görünmüyor. Aşağıda listeleme biçimim çalışır, ancak bazen de çöpleri toplar çünkü bir satırdaki "boşluklar" sayısında değişiklik yapar.

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; 
} 

Bunun için birkaç tane zarif çözüm gönderebilir misiniz lütfen? Bir şeyler yapmanın daha iyi yollarını öğrenmeye çalışıyorum ve rehberliği takdir ediyorum.

+0

bunu yapmak için bir komut satırı uygulaması yazıyorsanız, ben Perl gibi dize kullanım ve regexes daha uygun bir dil kullanarak öneririz:

Sonra ile sondan bir önceki öğeyi alabilirsiniz. Muhtemelen CPAN'da işlem tablosuna doğrudan erişmek için http://search.cpan.org/dist/Proc-ProcessTable/ –

+0

gibi bir veri bulabiliyordunuz. Veriler toplanmış ve bir sistem komut dosyasından metin dosyasına yazılmıştır. web uygulamasından exec ve root kullanmadan textfile'ı web uygulamam için erişilebilir hale getirir. ama sonuçları ayrıştırmak web uygulaması kendisi için yani php mantıklı. –

cevap

0

preg_match() kullanırım. Sistem yönetimi komut dosyalarının çoğuna benzer bir şey yapıyorum.

$test = "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"; 

$lines = explode("\n", $test); 

foreach($lines as $line){ 
     if(preg_match("/.php[\s+](cust[\d]+)[\s+]cron/i", $line, $matches)){ 
       print_r($matches); 
     } 

} 

yukarıdaki baskılar:

Array 
(
    [0] => .php cust1 cron 
    [1] => cust1 
) 
Array 
(
    [0] => .php cust3 cron 
    [1] => cust3 
) 
Array 
(
    [0] => .php cust4 cron 
    [1] => cust4 
) 
Array 
(
    [0] => .php cust89 cron 
    [1] => cust89 
) 
Array 
(
    [0] => .php cust435 cron 
    [1] => cust435 
) 
Array 
(
    [0] => .php cust16 cron 
    [1] => cust16 
) 

Çalıştırma işlemi çıktı eşit $test ayarlayabilirsiniz İşte bir örnek. Aradığınız değer, foreach altında if ifadesinde olur. $matches[1], custx değerine sahip olacaktır.

1

explode yerine preg_split kullanabilir ve [ ]+ (bir veya daha fazla boşluk) üzerinde bölünebilir.

preg_match_all('/[ ]php[ ]+\S+[ ]+(\S+)/', $input, $matches); 
$result = $matches[1]; 

desen bir boşluk, php, daha fazla boşluk olmayan boşluklar (yol) bir dizi, daha fazla boşluk maçlar ve ardından yakalar: Ama preg_match_all ve capturing ile gidebiliriz bu durumda düşünüyorum boşluk olmayan sonraki dizi. İlk boşluk çoğunlukla, kullanıcı adının bir parçası olarak php'u eşleştirmemenizi ve yalnızca bir komut olarak görmenizi sağlamak içindir.

Yakalama işlemine bir alternatif, PCRE'nin "sakla" özelliğidir. değerleri arasında kolunuz yeri sayısının değişken ise

preg_match_all('/[ ]php[ ]+\S+[ ]+\K\S+/', $input, $matches); 
$result = $matches[0]; 
0

, sen sadece olurdu preg_split

kullanabilirsiniz: Eğer desende \K kullanırsanız, her şey maçında atılır önce değiştirmek için:

$userProcess = explode(" ", $line); 

için:

$userProcess = preg_split("#\s+#", $line); 

\s+, bir veya daha fazla boşluk, sekme veya diğer beyaz boşluk karakterleriyle eşleşecektir.

$txt = $userProcess[count($userProcess) - 2];