When managing servers or debugging performance issues, system administrators often need to monitor specific processes like Java applications, Tomcat, Redis, MySQL, or MongoDB. The standard top command shows all running processes, which can be overwhelming when you're only interested in a few critical services.
By default, top displays all processes sorted by CPU usage:
top
This gives you a dynamic view of system activity, but finding your target processes requires manual scanning.
While top is running, you can use these interactive commands:
- Press 'o' or 'O' to add filter criteria
- Type 'COMMAND=java' to show only Java processes
- Use '|' for OR conditions: 'COMMAND=java|COMMAND=tomcat'
For a more permanent solution, use these approaches:
Method 1: Using pgrep with top
top -p $(pgrep -d',' java tomcat redis mysql mongod)
This shows only processes matching the specified names.
Method 2: Creating a Custom Top Configuration
Add this to your ~/.toprc file:
RCfile for "top with windows" # shameless bullshit line Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=3.0, Curwin=0 Def fieldscur=AEHIOQTWKNMbcdfgjplrsuvyzX winflags=62777, sortindx=10, maxtasks=0 summclr=6, msgsclr=6, headclr=7, taskclr=6 Job fieldscur=ABcefgjlrstuvyzMKNHIWOPQDX winflags=62777, sortindx=0, maxtasks=0 summclr=2, msgsclr=2, headclr=7, taskclr=6
Then run top with:
top -c -p $(pgrep -d',' java tomcat redis mysql mongod)
Consider these alternatives if you need more advanced filtering:
htop
htop --filter=java,tomcat,redis,mysql,mongod
Custom Script with Watch
Create a script called mytop:
#!/bin/bash watch -n 1 "ps aux | grep -E 'java|tomcat|redis|mysql|mongod'"
Make it executable and run:
chmod +x mytop ./mytop
For more precise filtering, you can monitor by:
- Process owner: top -u mysql
- Process group: top -g redis
- CPU threshold: top -b -n 1 | awk '$9 > 50'
For production environments, consider setting up monitoring with:
while true; do clear top -b -n 1 -p $(pgrep -d',' java tomcat redis mysql mongod) | head -n 20 sleep 2 done
When running top
in Linux, the default view shows all active processes. For developers managing specific services like Java applications, Tomcat, Redis, MySQL, or MongoDB, this can be overwhelming. Here's how to filter top
to focus only on the processes you care about.
The most straightforward method is using process IDs with the -p
flag:
# First get PIDs of your target processes pgrep -f 'java\|tomcat\|redis-server\|mysqld\|mongod' | tr '\n' ',' # Then run top with the filtered PIDs top -p $(pgrep -f 'java\|tomcat\|redis-server\|mysqld\|mongod' | tr '\n' ',')
Once top
is running, you can apply filters interactively:
1. Press 'o' (lowercase) to add a filter 2. Enter: COMMAND=java || COMMAND=tomcat || COMMAND=redis-server || COMMAND=mysqld || COMMAND=mongod 3. Press Enter to apply
For frequent use, create a .toprc
configuration file:
cat > ~/.toprc << 'EOF' RCfile for "top with windows" # shameless bullshit line Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=3.0, Curwin=0 Def fieldscur=AEHIOQTWKNMbcdfgGjlmrsuvySZ winflags=848024, sortindx=10, maxtasks=0 summclr=6, msgsclr=6, headclr=6, taskclr=6 Job fieldscur=ABcefgjlmyzMLONPHIKRWVZ winflags=62777, sortindx=0, maxtasks=0 summclr=6, msgsclr=6, headclr=6, taskclr=6 Filter fieldscur= winflags=130944, sortindx=10, maxtasks=50 summclr=1, msgsclr=1, headclr=1, taskclr=1 filter=COMMAND=java\|tomcat\|redis-server\|mysqld\|mongod EOF
If you can install htop
, it offers superior filtering capabilities:
# Install htop sudo apt install htop # Debian/Ubuntu sudo yum install htop # RHEL/CentOS # Run with filter htop --filter='java|tomcat|redis|mysql|mongo'
For continuous monitoring, combine with watch
:
watch -n 2 "top -b -n 1 -p \$(pgrep -f 'java\|tomcat\|redis-server\|mysqld\|mongod' | tr '\n' ',') | head -20"
For systemd-managed services, you might prefer:
watch -n 2 "systemctl status tomcat redis mysql mongod --no-pager"