How to find out if httpd is running or not via command line?

  • I'm working on a small control panel for my server. I need a command that will say if httpd is running or stopped.

    Will probably be using the same code for other services as well.

  • Oli

    Oli Correct answer

    6 years ago

    Most people run their httpd (Apache, Nginx, etc) through an init system. That's almost certainly the case if you've installed from a package. Almost all of these init systems have a method work working out if it's running. In my case I'm using nginx which ships a SysV-style init script and that accepts a status argument, like so:

    $ /etc/init.d/nginx status
     * nginx is running
    

    Obviously if you're running a different httpd, script or init system, you're going to have a slightly different syntax but unless you're manually launching the httpd yourself (which feels like the worst idea in the world), you're probably using a nice, managed start-up script that will allow you to query the status.

    slm's answer has more about this sort of init querying but the problem with trusting that is it only really tells you if a process is still running. Your httpd's main process could be running but in some way deadlocked. It makes a lot of sense to skip simple init tests and move on to behavioural tests.

    One thing we know about httpds is they listen. Usually on port *:80, but if yours doesn't, you can adapt the code following code. Here I'm just awking the output of netstat to see if it's listening on the right port.

    $ sudo netstat -ntlp | awk '$4=="0.0.0.0:80"'
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2079/nginx
    

    We could also check which process is running too to make sure the right httpd is running. We could do all sorts of checks. Depends how paranoid you want to be :)

    But even that is only a reflection of an httpd. Want to really test it? Well let's test it.

    $ wget --spider -S "http://localhost" 2>&1 | awk '/HTTP\// {print $2}'
    200
    

    I'm just looking at the response code (200 means "A-Okay!") but again, we could dig in and actually test the output to make sure it's being generated correctly.

    But even this isn't that thorough. You're checking localhost and it's reporting 200, nothing wrong? What if beavers chewed through the network cable that supplies the httpd (but not the rest of the system)? Then what?! You're reporting uptime when you're actually down. Few things look more unprofessional than incorrect status data.

    So let's talk to an external server (ideally on a completely different connection, in another galaxy far, far away) and ask it to query our server:

    $ ssh tank 'wget --spider -S "http://bert" 2>&1' | awk '/HTTP\// {print $2}'
    200
    

    By this point, any issues have reported are either in-app issues (which can have their own error -handling and -reporting, or they're at the client's end).

    A combination of these tests can help nail down where the issue is too.

    what happened to httpd?

    @Creek What do you mean?

    +1 for a good answer but OP asked about httpd and you used nginx

    @Creek In my books, "httpd" means "http daemon; a web server". Not a specific one. What do you think I should be doing differently? I don't know how I could check one any more thoroughly without needing to buy it a drink first.

    If I could buy you a drink for saying that I would, classic. I think OP meant apache's httpd

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM