Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. ps / top / htop

ps / top / htop

ps displays a snapshot of currently running processes. top and htop monitor process status and resource usage in real time. These commands are commonly used to check server load and identify specific processes.

Syntax

Basic usage of ps.

ps              # processes in the current shell
ps aux          # all processes for all users (most commonly used)
ps -ef          # all processes in full format
ps aux | grep process-name   # search for a specific process

Real-time monitoring with top.

top
top -u username   # show only processes for a specific user
top -p PID         # show only the process with a specific PID

htop is an enhanced version of top (requires installation).

htop

Commands and Options

CommandDescription
ps auxShows all processes for all users (a=all terminals, u=user format, x=include processes without a terminal).
ps -efShows all processes in full format, including PPID and start time.
ps aux --sort=-%cpuDisplays processes sorted by CPU usage in descending order.
ps aux --sort=-%memDisplays processes sorted by memory usage in descending order.
ps -p PIDShows information for the process with the specified PID.
pgrep nameSearches for a process by name and displays its PID.
pgrep -la nameDisplays the PID along with the process name.
topMonitors processes and system resources in real time.
top -bn1Runs once and exits immediately (batch mode, useful for scripts).
htopAn enhanced version of top with color output, mouse support, and tree view (requires installation).

Sample Code

The following examples assume a server running nginx and MySQL.

Displays all processes for all users. This is the most commonly used form.

ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY   STAT START   TIME COMMAND
root         1  0.0  0.1  19960  1532 ?     Ss   Mar05   0:01 /sbin/init
www-data  1234  0.0  0.2  55840  4096 ?     Ss   10:00   0:00 nginx: master
www-data  1235  0.0  0.1  55840  2048 ?     S    10:00   0:00 nginx: worker
mysql     2345  1.2  5.0 800000 50000 ?     Ssl  Mar05   2:30 /usr/sbin/mysqld

Searches for a specific process. The key trick is using grep -v grep to exclude the grep process itself from the results.

ps aux | grep nginx | grep -v grep
www-data  1234  0.0  0.2  55840  4096 ?  Ss  10:00  0:00 nginx: master
www-data  1235  0.0  0.1  55840  2048 ?  S   10:00  0:00 nginx: worker

Uses pgrep to find the PID of a process by name. The -la option also displays the command name.

pgrep -la nginx
1234 nginx: master process /etc/nginx/nginx.conf
1235 nginx: worker process

Displays the top 10 processes by highest CPU usage.

ps aux --sort=-%cpu | head -11

Displays the top 10 processes by highest memory usage.

ps aux --sort=-%mem | head -11

Checks whether a process is running inside a script. pgrep -x searches for processes whose name matches exactly.

check_mysql.sh
if pgrep -x "mysqld" > /dev/null; then
    echo "MySQL is running"
else
    echo "MySQL is not running"
fi
bash check_mysql.sh
MySQL is running

You can also enter an if statement directly in the terminal. After pressing Enter following then, a > prompt appears to indicate more input is expected. Enter fi to execute.

if pgrep -x "mysqld" > /dev/null; then
    echo "MySQL is running"
else
    echo "MySQL is not running"
fi
MySQL is running

Runs top once in batch mode. Useful for retrieving system status from a script.

top -bn1 | head -5
top - 12:00:00 up 1 day,  2:30,  1 user,  load average: 0.15, 0.10, 0.05
Tasks:  87 total,   1 running,  86 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.2 us,  0.5 sy,  0.0 ni, 98.0 id,  0.3 wa,  0.0 hi,  0.0 si
MiB Mem :   1024.0 total,    256.0 free,    512.0 used,    256.0 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.    400.0 avail Mem

Counts the number of running processes.

echo "Running processes: $(ps aux | wc -l)"
Running processes: 87

Notes

Key columns in ps aux output: USER (owner of the process), PID (process ID), %CPU (CPU usage), %MEM (memory usage), STAT (process state: R=running, S=sleeping, Z=zombie), COMMAND (command name).

To stop a process, use kill / pkill / killall. For background execution and job management, see & (background execution).

If you find any errors or copyright issues, please .