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.
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
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
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.
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
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:
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.
You can use the services command universally on most Linux distros.
This same command can be used for all services that are running on an individual basis or to find all the services' status.
The various methods within SysVinit, Systemd, and Upstart for listing services
If you're using one of the more typical service management frameworks you can use the following methods to list the services within each.