Understanding and Using the Linux ps Command
This article explains the Linux ps command, its purpose, common options, process states, and provides practical examples such as pstree, ps ‑Lf, pstack, kill signals, and various ps usages for listing all processes, filtering by user, and combining with grep.
The ps command (Process Status) lists a snapshot of the processes running on a Linux system at the moment it is executed. It is essential for monitoring and controlling processes, allowing users to see which processes are running, their states, resource usage, and more.
Command parameters:
a 显示所有进程
-a 显示同一终端下的所有程序
-A 显示所有进程
c 显示进程的真实名称
-N 反向选择
-e 等于“-A”
e 显示环境变量
f 显示程序间的关系
-H 显示树状结构
r 显示当前终端的进程
T 显示当前终端的所有程序
u 指定用户的所有进程
-au 显示较详细的资讯
-aux 显示所有包含其他使用者的行程
-C<命令> 列出指定命令的状况
--lines<行数> 每页显示的行数
--width<字符数> 每页显示的字符数
--help 显示帮助信息
--version 显示版本显示1. pstree
Displays processes in a tree structure.
$ pstree -p work | grep ad
sshd(22669)---bash(22670)---ad_preprocess(4551)-+-{ad_preprocess}(4552)
|-{ad_preprocess}(4553)
|-{ad_preprocess}(4554)
|-{ad_preprocess}(4555)
|-{ad_preprocess}(4556)
`-{ad_preprocess}(4557)In this example, the user work runs ad_preprocess with one main thread and six child threads.
2. ps -Lf
Shows detailed thread information.
$ ps -Lf 4551
UID PID PPID LWP C NLWP STIME TTY STAT TIME CMD
work 4551 22670 4551 2 7 16:30 pts/2 Sl+ 0:02 ./ad_preprocess
work 4551 22670 4552 0 7 16:30 pts/2 Sl+ 0:00 ./ad_preprocess
… (remaining lines omitted for brevity)The process has launched seven threads in total.
3. pstack
Displays the stack trace of each thread in a process.
$ pstack 4551
Thread 7 (Thread 1084229984 (LWP 4552)):
#0 0x000000302afc63dc in epoll_wait () from /lib64/tls/libc.so.6
#1 0x00000000006f0730 in ub::EPollEx::poll ()
… (remaining threads omitted for brevity)4. kill – terminating processes
Common signals for controlling processes:
kill -STOP [pid] # send SIGSTOP to pause a process
kill -CONT [pid] # send SIGCONT to resume a stopped process
kill -KILL [pid] # send SIGKILL to force immediate termination
kill -9 -1 # terminate all processes you ownSIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
5. Displaying all process information
Basic commands and examples:
ps -A – list all processes.
ps -u root – show processes owned by root .
ps -ef – list all processes with full command lines.
ps -ef|grep ssh – find specific processes using grep .
ps -l – display detailed information for the current session.
ps aux – show all processes with user, CPU, memory, and other columns.
ps -axjf – display a process tree.
ps aux | egrep '(cron|syslog)' – find PIDs related to cron or syslog services.
Each command’s output columns (USER, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT, START, TIME, COMMAND, etc.) are explained to help interpret the process state and resource consumption.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.