Skip to content

Commit 6e57a70

Browse files
authored
Merge pull request #423 from Nirzak/patch-3
Improved the ps command documentation
2 parents a4fba24 + d9746ea commit 6e57a70

File tree

1 file changed

+134
-18
lines changed

1 file changed

+134
-18
lines changed
Lines changed: 134 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,144 @@
11
# The `ps` command
22

3-
The `ps` command is used to identify programs and processes that are running on the system and the resources they are using.
4-
Its frequently [pipelined](<https://en.wikipedia.org/wiki/Pipeline_(Unix)>) with other commands like `grep` to search for a program/process or `less`
5-
so that the user can analyze the output one page at a time.
3+
The `ps` command (process status) is used to display information about running processes on a Linux system — such as their PID, memory usage, CPU time, and associated users.
64

7-
Let's say you have a program like openshot which is notorious for hogging system resources when exporting a video, and you want to close it, but the GUI has become unresponsive.
5+
It’s often **piped** with commands like `grep` to search for a specific process or `less` to scroll through large outputs.
86

9-
### Example
7+
## Why Use `ps`
108

11-
1. You want to find the PID of openshot and kill it.
9+
Imagine your system feels slow or an app becomes unresponsive — you can use `ps` to:
10+
- Identify processes consuming high CPU/memory
11+
- Find a program’s PID (Process ID)
12+
- Kill or debug a stuck process
13+
- Check who’s running what on a shared system
14+
15+
## Basic Syntax
16+
17+
```
18+
ps [options]
19+
```
20+
21+
Without any options, `ps` only shows processes in the current terminal session.
22+
23+
Example:
24+
```bash
25+
ps
26+
```
27+
28+
Output:
29+
```
30+
PID TTY TIME CMD
31+
4587 pts/0 00:00:00 bash
32+
4621 pts/0 00:00:00 ps
33+
```
34+
35+
## Essential Usage
36+
37+
**The one combo to remember:** `ps aux`
38+
- `a` = all processes (all users)
39+
- `u` = show user/owner info
40+
- `x` = include processes without terminals
41+
42+
```
43+
ps aux
44+
```
45+
46+
Output example:
47+
```
48+
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
49+
root 1 0.0 0.1 168208 1100 ? Ss 10:15 0:02 /sbin/init
50+
myuser 2471 0.5 1.2 431204 24500 ? Sl 10:17 1:05 code
51+
myuser 2523 2.3 0.7 230940 14860 pts/0 R+ 10:22 0:01 ps aux
52+
```
53+
54+
## Some More Practical Day to Day Examples
55+
56+
### Finding and Killing a process
57+
58+
You want to stop a frozen **OpenShot** process.
1259

1360
```
1461
ps aux | grep openshot
15-
kill - <openshot PID>
1662
```
1763

18-
2. To Show all the running processes:
64+
Output:
65+
```
66+
myuser 3625 99.9 6.1 1243924 252340 ? Rl 10:30 25:17 openshot
67+
myuser 3649 0.0 0.0 6348 740 pts/0 S+ 10:31 0:00 grep --color=auto openshot
68+
```
1969

70+
Now, kill it:
2071
```
21-
ps -A
72+
kill -9 3625
2273
```
2374

75+
### Show Processes by User
76+
```bash
77+
ps -u username
78+
```
2479

25-
### Syntax
80+
Output:
81+
```
82+
PID TTY TIME CMD
83+
2284 ? 00:00:00 sshd
84+
2455 ? 00:00:02 bash
85+
```
2686

27-
`ps [options]`
87+
### Filtering & Sorting Output
2888

29-
When run without any options, it's useless and will print: `CMD` - the executable processes/(program) running, their `PID` - process ID, `TTY` - terminal type and `Time` - How long the process has utilized the CPU or thread.
89+
Show top 10 memory-consuming processes:
90+
```
91+
ps aux --sort=-%mem | head -10
92+
```
93+
94+
Show top 10 CPU-consuming processes:
95+
```
96+
ps aux --sort=-%cpu | head -10
97+
```
98+
99+
### Checking Parent/Child Process Hierarchy
100+
```bash
101+
ps -ef --forest
102+
```
103+
This gives a tree-like structure showing parent-child relationships — useful when debugging service spawns.
104+
105+
### Custom Output Format
106+
To Show only PID, user, memory, and command:
107+
```
108+
ps -eo pid,user,%mem,cmd
109+
```
110+
111+
## Real-Life DevOps Examples
112+
113+
### 1. Checking which process uses a specific port
114+
```
115+
sudo ps -fp $(sudo lsof -t -i:8080)
116+
```
30117

31-
### Common Option
118+
### 2. Monitoring Jenkins, Nginx, or Docker processes
119+
```
120+
ps aux | grep nginx
121+
ps aux | grep jenkins
122+
ps aux | grep docker
123+
```
32124

33-
If you are going to remember only one thing from this page let it be these three letter `aux`:
34-
`a` - which displays all processes running, including those being run by other users.
35-
`u` - which shows the effective user of a process, i.e. the person whose file access permissions are used by the process.
36-
`x` - which shows processes that do not have a `TTY` associated with them.
125+
### 3. Find Zombie Processes
126+
```
127+
ps aux | awk '{ if ($8 == "Z") print $0; }'
128+
```
129+
130+
## Key Options for Quick Reference
131+
132+
| Option | Description |
133+
|:-------|:------------|
134+
| `aux` | All processes with detailed info |
135+
| `-ef` | Full listing (alternative to aux) |
136+
| `-eo format` | Custom output columns |
137+
| `--sort` | Sort by column (-%mem, -%cpu) |
138+
| `-p PID` | Show specific PID |
139+
| `-C name` | Show processes by command name |
140+
| `-u user` | Show user's processes |
141+
| `f` | ASCII art process tree |
37142

38143
### Additional Options:
39144

@@ -51,4 +156,15 @@ If you are going to remember only one thing from this page let it be these three
51156
|`--help simple`|Shows all the basic options|
52157
|`--help all`|Shows every available options|
53158

54-
Another useful command which give a realtime snapshot of the processes and the resources they are using about every ten seconds is `top`.
159+
## Related Tools
160+
161+
If you need **real-time** monitoring, use:
162+
```
163+
top
164+
```
165+
or the more user-friendly modern version:
166+
```
167+
htop
168+
```
169+
170+

0 commit comments

Comments
 (0)