Results 1 to 29 of 29
  1. #1

    Question HELP: Finding a process on a certain port

    Hey,

    This might seem like a weird question. I want to know if it's possible to find a certain process on a port using remote commands (SSH) on Linux.

    I want to do this because I want to make a script to manage SHOUTcast servers in PHP, but since there is no command to actually stop the SHOUTcast server from running, I'm in a bit of a problem.

    It's possible to find the process by searching on port 8000 because I've seen it in Webmin, and I've used that to stop the server previously.

    Any help would be appreciated, and if I don't make sense, just send a reply and I'll rethink how to type it.

    Thanks in advance,
    Jason

  2. #2
    netstat -napt will show the PID which you can stop by using kill.
    crucialparadigm - Affordable, Reliable, Professional :
    Web Hosting
    24/7 Support Web Hosting Reseller Hosting Cloud/VPS Plans Dedicated Servers

  3. #3
    Hmm yeah, just a bit tricky of choosing the right SHOUTcast server, because I'm looking to host multiple servers.

    Is it possible to find just processes listening through port 8000 (or 8002 or whatever)?

  4. #4
    netstat -napt | grep 8000

    Edit: You can only have one process listening to a port at any one time.
    crucialparadigm - Affordable, Reliable, Professional :
    Web Hosting
    24/7 Support Web Hosting Reseller Hosting Cloud/VPS Plans Dedicated Servers

  5. #5
    Thanks for your help crucialx!

    Hopefully I can work with this in PHP.

  6. #6
    Hmm, just one more question. Any idea how to make it just display the PID of the process. Because it seems to only output the first line in PHP, and the PID is on the second

  7. #7
    Just so you can see what I mean, here's a link:

    http://nitroxhosting.com/test2.php

  8. #8
    Join Date
    Apr 2005
    Location
    silicon and earthquakes
    Posts
    255
    This is what works for me:
    Code:
    netstat -napt | grep LISTEN | awk '/8000/ { print $7; }' | awk -F/ '{ print $1; }'
    First we search for everything listening on the machine - this filters out all current connections to the port. Then we just extract seventh column from the line that contains 8000. Since that seventh column is in the format of PID/PROCESS_NAME we split that again on slash and just display the first value, which is the PID.

  9. #9
    Ohh cool...Thanks a tonne. Now I should be able to have a successful script

  10. #10
    Hmm damn, these looks like a PHP problem now. It looks like it's cutting off the output where it has a - (dash)

  11. #11
    Join Date
    Apr 2005
    Location
    silicon and earthquakes
    Posts
    255
    Post your PHP code snippet. It might be character escaping.

  12. #12
    Join Date
    Jun 2003
    Location
    UK
    Posts
    6,601
    fuser 8888/tcp might also be userful. Very handy command

    Rus
    Russ Foster - Industry Curmudgeon

  13. #13
    PHP Code:
    <?php

    echo "Checking ports for SHOUTcast<br><br>";

    echo 
    "Found SHOUTcast on port 8000 with PID "exec("netstat -napt | grep LISTEN | awk '/8000/ { print $7; }' | awk -F/ '{ print $1; }'");

    echo 
    "<br><br>Checking complete!";

    ?>
    That's the PHP code I am using
    Last edited by Jason.NXH; 04-28-2005 at 11:40 AM.

  14. #14
    Join Date
    Apr 2005
    Location
    silicon and earthquakes
    Posts
    255
    I assume that you set up your sudoers to allow user apache to run netstat, right?

    Does it work from the command line when you are root? And when you are apache?

    Also, try deleting everyting after netstat -napt and see what the output is then (if any). If there is none there is likely a permissions problem (sudoers).

  15. #15
    I didn't do anything with the sudoers. The command works when I run as root through shell and I haven't tried with apache.

    If I delete the rest of the netstat, it only shows the first line...and ends with a -, but doesn't show a second line. Also, I figured the full command appears as a minus because that's what it outputs when not in root, but I don't know how to put the command through PHP as a different user

  16. #16
    Originally posted by Jason.NXH
    I didn't do anything with the sudoers. The command works when I run as root through shell and I haven't tried with apache.

    If I delete the rest of the netstat, it only shows the first line...and ends with a -, but doesn't show a second line. Also, I figured the full command appears as a minus because that's what it outputs when not in root, but I don't know how to put the command through PHP as a different user
    You need to setup sudoers because PHP runs all the commands through the apache user. The apache user does not have all of the root privilages by default hence why you should look into sudoers.

    man sudo
    man sudoers

  17. #17
    Althrough this isn't very secure you could just follow these steps:

    1. visudo
    2. look for: root ALL=(ALL) ALL
    3. right below it put: apache ALL=(ALL) ALL
    4. save

    keep in mind giving apache all of root's privilages is very insecure but it does the job if you're just messing around.

  18. #18
    Join Date
    Apr 2005
    Location
    silicon and earthquakes
    Posts
    255
    You can test that script as user apache by executing

    su - apache

    while being root.

    Confirm that you actually switched to user apache by issuing whoami and verifying that you are not root.

    Actually, depending on the distribution that you are using the apache web server might run as user nobody, so substitute nobody for apache in the above example.

    When you confirmed you are the same user as apache webserver is running issue
    whoami; sudo netstat -napt
    and post the response here.

  19. #19
    I have changed the command line, but now it shows 'nobody' as the PID.

    PHP Code:
    <?php

    echo "Checking ports for SHOUTcast<br><br>";

    echo 
    "Found SHOUTcast on port 8000 with PID "exec("whoami; sudo netstat -napt | grep LISTEN | awk '/8000/ { 

    print $7; }' | awk -F/ '{ print $1; }'"
    );

    echo 
    "<br><br>Checking complete!";

    ?>
    Where do I place su - apache, in the command line in PHP? or in my SSH console?

  20. #20
    Join Date
    Apr 2005
    Location
    silicon and earthquakes
    Posts
    255
    Did you do what LinuxMaster suggested? If not try the following. Open up your root shell and type
    visudo
    Go to the end of this file and add the following line:
    Code:
    nobody ALL=(root) NOPASSWD: /bin/netstat
    (its slightly more secure), save the file and try the following.

    What I meant initially was that you execute
    whoami; sudo netstat -napt
    in your shell after you switch users to nobody.

    So. Open up your root shell window again, issue
    su - nobody
    and then issue
    whoami; sudo netstat -napt
    What output do you get now?

  21. #21
    I have entered the code into the sudoers file, now when I type 'whoami; sudo netstat -napt' into the shell, I get the following:

    Code:
    root@**** [/etc]# whoami; sudo netstat -napt
    root
    sudo: /etc/sudoers is owned by gid 32004, should be 0

  22. #22
    Join Date
    Apr 2005
    Location
    silicon and earthquakes
    Posts
    255
    You cannot edit sudoers file by hand. You really need to use visudo to do the editing stuff.

    Try
    chown root:root /etc/sudoers
    to fix the problem and then issue
    su - nobody
    whoami; sudo netstat -napt

  23. #23
    I changed the owner, but all I got was:

    Code:
    [email protected] [~]# su - nobody
    This account is currently not available.
    [email protected] [~]# whoami; sudo netstat -napt
    root
    >>> sudoers file: syntax error, line 7 <<<
    >>> sudoers file: syntax error, line 9 <<<
    >>> sudoers file: syntax error, line 11 <<<
    >>> sudoers file: syntax error, line 13 <<<
    >>> sudoers file: syntax error, line 15 <<<
    >>> sudoers file: syntax error, line 16 <<<
    >>> sudoers file: syntax error, line 19 <<<
    >>> sudoers file: syntax error, line 22 <<<
    >>> sudoers file: syntax error, line 25 <<<
    >>> sudoers file: syntax error, line 29 <<<
    sudo: parse error in /etc/sudoers near line 7

  24. #24
    Join Date
    Apr 2005
    Location
    silicon and earthquakes
    Posts
    255
    Sorry Jason, I am out of town till Wednesday with no access to shell

    I'd try to restore sudoers file (uninstall sudo, remove /etc/sudoers, install sudo) then edit it again with visudo.

  25. #25
    Join Date
    Nov 2004
    Location
    England
    Posts
    513
    How about just setting up a cron to run every minute and output to a text file, and just read the text file in through PHP? Helluva lot simpler!

  26. #26
    Join Date
    Apr 2005
    Location
    silicon and earthquakes
    Posts
    255
    Cron job might work if he does not need realtime PID info. The info a cron job would write to a file might be delayed upto a minute.

    I don't know the behavior of a shoutcast server, but since it is probably just a listening daemon its PID will remain constant (much like Apache PID, the one that is owned by root).

  27. #27
    Join Date
    Nov 2004
    Location
    England
    Posts
    513
    Originally posted by rotoiti
    Cron job might work if he does not need realtime PID info. The info a cron job would write to a file might be delayed upto a minute.

    I don't know the behavior of a shoutcast server, but since it is probably just a listening daemon its PID will remain constant (much like Apache PID, the one that is owned by root).
    Yes, that's what I think too. Not actually certain though

  28. #28
    I can find the PID of a single shoutcast server using 'pidof', but since I want to host multiple shoutcast servers, it's going to be a problem determining which PID belonds to which server, hence I wanted to find the PID of a process on a port.

  29. #29
    Hmm anyways, thanks for all of your help However I may not make this script (for now anyway), because of the access risks involved as it is a web hosting server.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •