PHP passthru oder system Befehl zur Wiedergabe von 'top'

bra-tak

Lt. Junior Grade
Registriert
Apr. 2009
Beiträge
418
Guten Tag allesamt.

Ich bin gerade am Feinschliff meines Raspberry Pi Servers, und will mir auf meiner Website die aktiven Prozesse über den 'top' Linux-Befehl anzeigen lassen. Momentan nutze ich folgenden Code:

PHP:
echo "<font color=\"#FFFFFF\"><h4>Aktive Prozesse:</h4></font>"; 
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
exec('/usr/bin/top -b -n 1', $return1);
print_r($return1);

Das return1 muss meines Wissen so sein, da davor schon ein return aufgrufen wird. Es wird mir auch der Inhalt von 'top' angezeigt, allerding völlig unformatiert.

Weiß jemand, wie ich die Formatierung so einstellen kann, dass sie so, wie in der Shell aussieht? Momentan siehts so aus:

Aktive Prozesse:
Array ( [0] => top - 14:41:01 up 1 day, 19:55, 1 user, load average: 0.98, 1.21, 1.67 [1] => Tasks: 109 total, 1 running, 106 sleeping, 2 stopped, 0 zombie [2] => %Cpu(s): 6.2 us, 3.0 sy, 0.0 ni, 82.6 id, 8.1 wa, 0.0 hi, 0.1 si, 0.0 st [3] => KiB Mem: 448776 total, 324216 used, 124560 free, 16276 buffers [4] => KiB Swap: 1048572 total, 155356 used, 893216 free, 68928 cached [5] => [6] => PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND [7] => 27798 www-data 20 0 77888 10m 6700 D 30.5 2.3 0:03.11 apache2 [8] => 27881 www-data 20 0 3128 1116 832 R 13.1 0.2 0:00.07 top [9] => 1 root 20 0 2140 556 528 S 0.0 0.1 0:06.74 init [10] => 2 root 20 0 0 0 0 S 0.0 0.0 0:00.07 kthreadd [11] => 3 root 20 0 0 0 0 S 0.0 0.0 0:22.82 ksoftirqd/0 [12] => 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H [13] => 7 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/u:0H [14] => 8 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper [15] => 9 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kdevtmpfs [16] => 10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns [17] => 12 root 20 0 0 0 0 S 0.0 0.0 0:00.03 bdi-default [18] => 13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kblockd [19] => 14 root 20 0 0 0 0 S 0.0 0.0 0:00.50 khubd [20] => 15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 rpciod [21] => 16 root 20 0 0 0 0 S 0.0 0.0 0:00.17 khungtaskd [22] => 17 root 20 0 0 0 0 S 0.0 0.0 0:16.52 kswapd0 [23] => 18 root 20 0 0 0 0 S 0.0 0.0 0:00.00 fsnotify_mark [24] => 19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 nfsiod [25] => 20 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 crypto [26] => 27 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kthrotld [27] => 28 root 1 -19 0 0 0 S 0.0 0.0 0:00.00 VCHIQ-0 [28] => 29 root 1 -19 0 0 0 S 0.0 0.0 0:00.00 VCHIQr-0 [29] => 30 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 VCHIQs-0 [30] => 31 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 iscsi_eh [31] => 32 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 dwc_otg [32] => 33 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 DWC Notificatio [33] => 35 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 deferwq [34] => 37 root 20 0 0 0 0 D 0.0 0.0 28:57.42 mmcqd/0 [35] => 38 root 20 0 0 0 0 D 0.0 0.0 0:12.70 jbd2/mmcblk0p2- [36] => 39 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ext4-dio-unwrit [37] => 154 root 20 0 2876 580 500 S 0.0 0.1 0:00.50 udevd [38] => 163 root 20 0 0 0 0 S 0.0 0.0 0:00.00 scsi_eh_0 [39] => 165 root 20 0 0 0 0 S 0.0 0.0 0:00.12 usb-storage [40] => 181 root 20 0 0 0 0 S 0.0 0.0 0:00.00 scsi_eh_1 [41] => 182 root 20 0 0 0 0 S 0.0 0.0 0:04.56 usb-storage [42] => 288 root 20 0 2872 476 344 S 0.0 0.1 0:00.02 udevd [43] => 336 root 20 0 0 0 0 S 0.0 0.0 0:26.97 flush-179:0 [44] => 495 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 md [45] => 782 root 20 0 2872 284 280 S 0.0 0.1 0:00.00 udevd [46] => 1451 root 20 0 0 0 0 S 0.0 0.0 0:02.74 jbd2/sdb1-8 [47] => 1452 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ext4-dio-unwrit [48] => 1462 root 20 0 0 0 0 S 0.0 0.0 0:00.00 jbd2/sda1-8 [49] => 1463 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ext4-dio-unwrit [50] => 1822 root 20 0 1740 404 384 S 0.0 0.1 1:28.43 ifplugd [51] => 1843 root 20 0 1740 404 384 S 0.0 0.1 0:14.43 ifplugd [52] => 2236 root 20 0 27976 1104 828 S 0.0 0.2 2:06.47 rsyslogd [53] => 2298 root 20 0 1700 480 476 S 0.0 0.1 0:00.00 acpid [54] => 2404 root 20 0 4888 400 292 S 0.0 0.1 0:00.01 dhclient [55] => 2488 root 20 0 74804 2028 1096 S 0.0 0.5 0:19.79 apache2 [56] => 2495 www-data 20 0 23316 864 376 S 0.0 0.2 0:08.09 apache2 [57] => 2562 messageb 20 0 3172 832 672 S 0.0 0.2 0:00.37 dbus-daemon [58] => 2590 avahi 20 0 3376 860 780 S 0.0 0.2 0:00.09 avahi-daemon [59] => 2591 avahi 20 0 3376 184 172 S 0.0 0.0 0:00.00 avahi-daemon [60] => 2734 root 20 0 61676 1036 732 S 0.0 0.2 4:44.22 collectd [61] => 2766 root 20 0 2612 344 308 S 0.0 0.1 0:00.09 mdadm [62] => 2819 root 20 0 1748 380 376 S 0.0 0.1 0:00.11 mysqld_safe [63] => 3162 mysql 20 0 312m 44m 3060 S 0.0 10.2 20:24.57 mysqld [64] => 3163 root 20 0 1840 488 484 S 0.0 0.1 0:00.00 logger [65] => 3334 root 20 0 6204 636 540 S 0.0 0.1 0:00.03 sshd [66] => 3368 nobody 20 0 2008 476 456 S 0.0 0.1 0:02.95 thd [67] => 3377 root 20 0 4208 448 396 S 0.0 0.1 0:00.03 vsftpd [68] => 3397 root 20 0 13320 884 724 S 0.0 0.2 4:26.80 monit [69] => 3526 root -2 0 1728 1668 1348 S 0.0 0.4 1:01.74 watchdog [70] => 3554 root 20 0 178m 9584 1932 S 0.0 2.1 3:37.40 python [71] => 3599 root 20 0 9904 888 800 S 0.0 0.2 0:07.65 nmbd [72] => 3602 root 20 0 18864 1160 952 S 0.0 0.3 0:02.06 smbd [73] => 3611 root 20 0 19380 188 152 S 0.0 0.0 0:00.00 smbd [74] => 3640 root 20 0 3736 568 564 S 0.0 0.1 0:00.05 getty [75] => 3641 root 20 0 3736 568 564 S 0.0 0.1 0:00.03 getty [76] => 3642 root 20 0 3736 568 564 S 0.0 0.1 0:00.03 getty [77] => 3643 root 20 0 3736 568 564 S 0.0 0.1 0:00.03 getty [78] => 3644 root 20 0 3736 568 564 S 0.0 0.1 0:00.03 getty [79] => 3645 root 20 0 3736 568 564 S 0.0 0.1 0:00.03 getty [80] => 3646 root 20 0 2056 540 536 S 0.0 0.1 0:00.01 getty [81] => 3669 root 20 0 27516 2032 1692 S 0.0 0.5 0:01.43 console-kit-dae [82] => 3736 root 20 0 22280 1620 1320 S 0.0 0.4 0:00.61 polkitd [83] => 3803 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:1H [84] => 4808 www-data 20 0 176m 17m 10m S 0.0 4.0 6:09.49 apache2 [85] => 6432 www-data 20 0 177m 115m 11m S 0.0 26.4 6:06.74 apache2 [86] => 15201 root 20 0 0 0 0 S 0.0 0.0 0:10.41 kworker/u:2 [87] => 17322 www-data 20 0 77776 15m 11m S 0.0 3.4 2:50.11 apache2 [88] => 19338 root 20 0 3816 692 632 S 0.0 0.2 0:00.46 cron [89] => 25251 root 20 0 1668 428 392 S 0.0 0.1 0:02.03 hd-idle [90] => 26443 www-data 20 0 78008 15m 12m S 0.0 3.6 1:43.21 apache2 [91] => 26594 www-data 20 0 77616 14m 10m S 0.0 3.3 0:54.59 apache2 [92] => 26748 www-data 20 0 81852 19m 12m S 0.0 4.5 1:38.94 apache2 [93] => 26749 www-data 20 0 78044 13m 10m S 0.0 3.2 0:42.47 apache2 [94] => 26849 www-data 20 0 77752 12m 9396 S 0.0 2.9 0:35.67 apache2 [95] => 27669 nobody 20 0 23572 3452 2900 S 0.0 0.8 0:02.96 smbd [96] => 27671 root 20 0 0 0 0 S 0.0 0.0 0:00.08 flush-8:16 [97] => 27684 www-data 20 0 77624 12m 9300 S 0.0 2.8 0:50.55 apache2 [98] => 27685 root 20 0 9840 3196 2568 S 0.0 0.7 0:00.51 sshd [99] => 27692 pi 20 0 9840 1628 992 S 0.0 0.4 0:00.71 sshd [100] => 27693 pi 20 0 6180 3316 1504 S 0.0 0.7 0:01.15 bash [101] => 27708 root 20 0 5112 1600 1288 S 0.0 0.4 0:00.04 sudo [102] => 27709 root 20 0 4932 1504 1200 S 0.0 0.3 0:00.04 su [103] => 27716 root 20 0 4772 1844 1436 S 0.0 0.4 0:00.07 bash [104] => 27718 root 20 0 9352 3780 2292 T 0.0 0.8 0:04.80 mc [105] => 27720 root 20 0 4772 1844 1436 S 0.0 0.4 0:00.34 bash [106] => 27754 root 20 0 8820 3172 2220 T 0.0 0.7 0:00.23 mc [107] => 27756 root 20 0 4768 1836 1436 S 0.0 0.4 0:00.06 bash [108] => 27759 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/u:0 [109] => 27766 root 20 0 9276 3484 2272 S 0.0 0.8 0:04.54 mc [110] => 27768 root 20 0 4768 1840 1436 S 0.0 0.4 0:00.16 bash [111] => 27797 root 20 0 0 0 0 S 0.0 0.0 0:00.11 kworker/0:1 [112] => 27806 root 20 0 0 0 0 S 0.0 0.0 0:00.07 kworker/0:2 [113] => 27872 root 20 0 0 0 0 S 0.0 0.0 0:00.03 kworker/0:0 [114] => 27877 www-data 20 0 74828 1656 656 S 0.0 0.4 0:00.00 apache2 [115] => 27880 www-data 20 0 1748 516 448 S 0.0 0.1 0:00.00 sh )
 
Wozu exec wenn es den Backtick-Operator gibt? Nutz lieber $ausgabe = `top`;. Newlines werden dort drin sein, die Browser scheren sich aber nen Dreck um Zeilenumbrüche. Nutz deshalb einfach nl2br, falls du Newlines zu Zeilenumbrüchen (<br>) umwandeln willst.

Alternativ ginge es natürlich auch mit <pre>. Kommt drauf an was du damit anstellen willst.
 
Anscheinend hast du ein Array mit den Zeilen, versuch' also einfach
Code:
foreach ($return1 as $line) print("$line\n");

/edit: Tu was yuuri sagt falls du HTML ausgibst. Und ja, eigentlich gibt es keinen guten Grund exec zu verwenden.
 
Hallo, danke euch schonmal.

Wenn ich es so schreibe, bekomme ich gar keine Ausgabe. (Ich bin in php nicht der bewandertste, also nehmt mir banale Fehler nicht zu übel ;))

PHP:
echo "<font color=\"#FFFFFF\"><h4>Aktive Prozesse:</h4></font>"; 
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
$ausgabe = `top`;

PS: Es handelt sich um eine .php Datei, nur falls das von Relevanz ist.
 
ich habe deine beiden Varianten probiert, bekomme aber keine Ausgabe. Irgendwas scheine ich noch falsch zu machen.
Ergänzung ()

noch Ideen?
Ergänzung ()

Habe es lösen können :)

PHP:
echo "<pre>";
passthru('/usr/bin/top -b -n 1');
echo "</pre>";
 
Zuletzt bearbeitet:
Zurück
Oben