How to find out if httpd is running or not via command line?
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 awk
ing 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.
You can use the services command universally on most Linux distros.
$ service <service> status
Example
$ service httpd status
httpd (pid 23569) is running...
This same command can be used for all services that are running on an individual basis or to find all the services' status.
$ service --status-all
python is stopped
automount (pid 22457) is running...
Avahi daemon is not running
Avahi DNS daemon is not running
crond (pid 23577) is running...
gpm is stopped
hald is stopped
httpd (pid 23569) is running...
...
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.
SysVinit
$ ls -l /etc/init.d/ | head -10
total 220
-rwxr-xr-x 1 root root 1422 Jan 13 2009 ajaxterm
-rwxr-xr-x 1 root root 3052 Apr 20 2012 autofs
-rwxr-xr-x 1 root root 1877 Apr 13 2011 avahi-daemon
-rwxr-xr-x 1 root root 1824 Apr 13 2011 avahi-dnsconfd
-rwxr-xr-x 1 root root 1926 Feb 22 2012 crond
-rwxr-xr-x 1 root root 14291 Dec 19 2011 functions
-rwxr-xr-x 1 root root 1778 Jan 6 2007 gpm
-rwxr-xr-x 1 root root 1586 Mar 5 2011 haldaemon
-rwxr-xr-x 1 root root 5742 Dec 19 2011 halt
Systemd
$ systemctl list-unit-files --type=service | head -10
UNIT FILE STATE
abrt-ccpp.service enabled
abrt-oops.service enabled
abrt-pstoreoops.service disabled
abrt-vmcore.service enabled
abrt-xorg.service enabled
abrtd.service enabled
accounts-daemon.service enabled
alsa-restore.service static
alsa-state.service static
Upstart
$ initctl list | head -10
avahi-daemon start/running, process 1090
mountall-net stop/waiting
nmbd start/running, process 2045
passwd stop/waiting
rc stop/waiting
rsyslog start/running, process 1088
tty4 start/running, process 1211
udev start/running, process 483
upstart-udev-bridge start/running, process 480
ureadahead-other stop/waiting
References
- SysVinit to Systemd Cheatsheet
- Command to list services that start on startup?