Get running processes ?

Discussion in 'PHP' started by thesurface, Jan 10, 2014.

  1. #1
    I tried this way:
    <?php
    exec("ps aux" , $psOutput);
    print_r($psOutput);
    ?>

    ALso tried even only ps

    But all i got as output was:
        [0] => USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
        [1] => apache    1102  0.0  0.0   2288   508 ?        R    13:13   0:00 ps aux
        [2] => apache    9035  0.0  0.6  29012 12600 ?        S    Jan09   1:05 /usr/sbin/httpd
        [3] => apache    9036  0.1  0.6  28636 12380 ?        S    Jan09   1:08 /usr/sbin/httpd
        [4] => apache    9037  0.1  0.6  28892 12720 ?        S    Jan09   1:21 /usr/sbin/httpd
        [5] => apache    9038  0.1  0.6  28892 12532 ?        S    Jan09   1:10 /usr/sbin/httpd
        [6] => apache    9039  0.1  0.5  27632 11208 ?        S    Jan09   1:10 /usr/sbin/httpd
        [7] => apache    9040  0.1  0.7  31560 15188 ?        S    Jan09   1:09 /usr/sbin/httpd
        [8] => apache    9041  0.0  0.6  28892 12388 ?        S    Jan09   0:58 /usr/sbin/httpd
        [9] => apache    9042  0.0  0.4  26448  9836 ?        S    Jan09   0:57 /usr/sbin/httpd
        [10] => apache    9299  0.1  0.4  26448  9996 ?        S    Jan09   1:09 /usr/sbin/httpd
        [11] => apache    9300  0.1  0.6  29228 12748 ?        S    Jan09   1:15 /usr/sbin/httpd
        [12] => apache    9301  0.1  0.6  29308 12800 ?        S    Jan09   1:09 /usr/sbin/httpd
    Code (markup):
    Where is all other processes ??
     
    thesurface, Jan 10, 2014 IP
  2. dayvo

    dayvo Active Member

    Messages:
    585
    Likes Received:
    52
    Best Answers:
    1
    Trophy Points:
    55
    #2
    The script will be running as user nobody or your webserver user - which is why all your seeing is apache..

    I dont know how to do it, but you would need the script to be able to run as as user with permission to see all of the running processes - perhaps root (probably not the best idea) or by using sudo somehow?
     
    dayvo, Jan 10, 2014 IP
  3. thesurface

    thesurface Active Member

    Messages:
    260
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #3
    I use centos 5.9 if that helps, but i think there is no way to do it, seems like its somehow protecting me from viewing other things like processes.

    I also tried another way that php calling shell script, and inside *.sh script is ps aux but no luck.
     
    thesurface, Jan 10, 2014 IP
  4. dayvo

    dayvo Active Member

    Messages:
    585
    Likes Received:
    52
    Best Answers:
    1
    Trophy Points:
    55
    #4
    The problem is that the user issuing the 'ps aux' command is your webserver user - usually nobody. The user nobody doesnt have permission to view any running processes other than its own.

    I thought you might be able to structure your code somehow to have the command run as a different user, but this is something im not familiar with so its nothing more than a guess.

    However I just did a quick search and found this:

    "You should also bear in mind that PHP will not run as a process (unless its CLI) as it will be invoked from apache, or your web server"

    If you can put apache in to cgi mode this should allow you to achieve what your trying, but may mean compatibility with other scripts or sites running under apache may be affected.
     
    dayvo, Jan 10, 2014 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    ThePHPMaster, Jan 10, 2014 IP
  6. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #6
    Just out of curiosity why do you need to fetch all running processes from a PHP script executed by the web server? If you were creating some sort of administration script couldn't you run the php script locally via command line?
     
    NetStar, Jan 10, 2014 IP
  7. dayvo

    dayvo Active Member

    Messages:
    585
    Likes Received:
    52
    Best Answers:
    1
    Trophy Points:
    55
    #7
    I don't think running php as root is a good idea tbh
     
    dayvo, Jan 12, 2014 IP