Understanding Process Discrepancy: Why htop Shows More Apache2 Processes Than ps aux


35 views

When monitoring Apache performance on Ubuntu servers, many administrators notice an interesting discrepancy between process monitoring tools. While ps aux | grep apache might show just a handful of processes, htop often displays dozens. Let's dissect why this happens.

The core reason lies in how these tools handle threads differently:

# Traditional process view (ps)
ps aux | grep apache | wc -l
# Might return: 5

# Thread-aware view (htop)
htop --filter=apache | wc -l
# Might return: 25

Apache's worker model (especially with mpm_event or mpm_worker) creates multiple threads per process. Here's how to see this explicitly:

ps -eLf | grep apache  # Shows threads with LWP column
top -H                 # Threads view in top

Modern Apache uses these multi-processing modules:

  • mpm_prefork: Creates multiple processes (shows in both ps and htop)
  • mpm_worker/mpm_event: Creates processes with multiple threads

Verify your MPM with:

apache2ctl -V | grep -i mpm

When troubleshooting, consider these approaches:

# View all threads including their CPU usage
ps -eLf --sort=-%cpu | head -20

# Alternative using htop filters
F4 → type "apache" → shows all matching threads

For accurate process counting in scripts:

# Count Apache master + worker processes
pgrep -c apache2

# Count all threads (including workers)
pgrep -c -P $(pgrep -o apache2)

For deeper analysis of thread relationships:

# Show process tree with threads
pstree -p $(pgrep apache2)

# View thread-specific stats
cat /proc/$(pgrep apache2 | head -1)/task/*/status | grep -i thread

Remember that thread counts will vary based on:

  • ServerLimit and ThreadsPerChild directives
  • Current request load
  • Keepalive connections

When troubleshooting Apache performance on Ubuntu servers, administrators often encounter differences between process counts reported by ps aux and htop. Here's what's happening under the hood:

# Typical ps output showing parent Apache processes
ps aux | grep apache | grep -v grep
www-data   1234  0.0  1.2 123456 78900 ?        S    Jul01   0:10 /usr/sbin/apache2 -k start
www-data   1235  0.0  1.3 123789 79234 ?        S    Jul01   0:11 /usr/sbin/apache2 -k start

Apache uses either the prefork or worker MPM (Multi-Processing Module), which creates child processes or threads to handle requests:

# Check your current MPM
apache2ctl -V | grep -i mpm

# Sample output for prefork MPM:
Server MPM:     prefork

htop displays all threads by default, while ps shows summarized process trees. To make ps show threads similarly:

# Show threads with ps
ps -eLf | grep apache

# Alternative with full details
ps auxH | grep apache

Here's how to accurately count Apache processes/threads:

# Count parent and child processes
ps -ef | grep apache | grep -v grep | wc -l

# Count including threads (matches htop)
ps -eLf | grep apache | grep -v grep | wc -l

Apache's mpm_prefork.conf or mpm_worker.conf directly affects what you see:

# Typical prefork configuration showing process creation
<IfModule mpm_prefork_module>
    StartServers            5
    MinSpareServers         5
    MaxSpareServers        10
    MaxRequestWorkers     150
    MaxConnectionsPerChild   0
</IfModule>

For real-time monitoring, consider these alternatives:

# Use top in thread mode
top -H -p $(pgrep apache2)

# Or use Apache's built-in status
apache2ctl status