4

How can I find the uptime of Apache2. There are numerous suggestions out there to use

httpd fullstatus

But this fails

-bash: httpd: command not found

There is no httpd command running in top either, just an apache2 process.

I also tried (without any luck)

httpd status
apache2 status
apache2 fullstatus
/usr/sbin/apache2 fullstatus
/usr/sbin/apache2 status

What command can I use to find apache2's uptime reliably given the differences in how Apache is installed/configured on various platforms?

voretaq7
  • 79,345
  • 17
  • 128
  • 213
Jake N
  • 243
  • 2
  • 3
  • 9
  • To what precision do you need the answer? Will +/- 1 day do? – MadHatter Oct 25 '13 at 10:17
  • Yes MadHatter. Basically I want to know was it started just now (5 seconds) or some time ago. Its to check that the restart command actually ran. – Jake N Oct 25 '13 at 10:17

3 Answers3

18

You can use ps -eo comm,etime for example:

$ ps -eo comm,etime | grep httpd
httpd            5-01:40:22

This shows the elapsed time since the process was started. Mine is showing 5 days, 1 hr, 40 mins, 22 secs.

And after a restart:

$ ps -eo comm,etime | grep httpd
httpd                 00:07

In a graceful restart, if an apache process is still serving a connection it won't be killed until it finishes, so if it's a large download to a slow host, it may linger for a while until it completes.

I did snip most of the output as it shows each forked process, but you will have a general overview of how long it has been running.

Regan
  • 1,011
  • 1
  • 7
  • 15
  • Another version of this worked for me `ps --no-headers -o etime -C apache2` – Jake N Oct 25 '13 at 12:11
  • 1
    That was originally what I was going to post (`ps -o etime -C httpd`), but when I tested it, I typo'ed so it didn't work correctly, so I showed the grep version instead thinking something wasn't working as I thought it would. Glad you got it to work! – Regan Oct 25 '13 at 23:44
  • I used @MadHatter's `netstat` trick (below) to figure out that I should be grepping for `nginx` using this command. Got my uptime! :-) – KlaymenDK Feb 13 '18 at 09:46
7

I suspect there are lots of apache-version-specific ways to do this, but one generally-valid (and slightly brute-force) way is as follows.

Find the PID of the process that has the port open; it will have been the first one started:

[root@lory ~]# netstat -apn|grep -w 80|grep LISTEN
tcp        0      0 :::80                       :::*                        LISTEN      2903/httpd          

The netstat command requires privilege, so run as root or under sudo. In this case the PID is 2903. Then find how long that process has been running:

[root@lory ~]# ps auxww|grep 2903|grep -v grep
apache    2903  0.0  1.0 413316 39972 ?        S    Oct23   0:42 /usr/sbin/httpd

In this case, the start time is Oct 23, so I've been running a couple of days. You might get a field like 11:15, in which case it's a time of day, today, so you know the restart's worked.

MadHatter
  • 78,442
  • 20
  • 178
  • 229
6

If you have mod_status installed you should be able to determine the uptime with:

apachectl status

Which will show the current time as well as the last restart time. It might be version/dstro specific, but my debian servers show "Server uptime" in the output as well

Mathias R. Jessen
  • 24,907
  • 4
  • 62
  • 95
  • I do not have it installed, but good call +1 – Jake N Oct 25 '13 at 12:12
  • 1
    You also don't have to use `apachectl` -- `http://127.0.0.1/status` (or wherever mod_status is configured - localhost is the default) will get you the same thing if the module is installed & configured to accept requests from your location. We allow our monitoring system to get the status page on our internal servers for convenience. – voretaq7 Oct 25 '13 at 18:08