ps
1. ps (Process Status)
ps is a command used to show information about the currently running processes.
Syntax:
ps [options]
Common Options:
-eor-A: Show all processes.-f: Full format listing.-u username: Show processes for a specific user.-aux: Show all processes, including those from other users.
Example:
ps aux
This shows a full list of processes running on the system.
Sample Output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 169448 6092 ? Ss 10:15 0:03 /sbin/init
user 1234 1.2 0.3 254548 12576 pts/0 Ss+ 10:45 0:10 bash
user 1300 0.0 0.1 123456 6789 pts/0 S+ 10:47 0:00 vim myfile.txt
- Explanation:
PID: Process ID.%CPU: CPU usage percentage.%MEM: Memory usage percentage.VSZ: Virtual memory size.RSS: Resident Set Size (physical memory).TTY: Terminal associated with the process.STAT: Process status (e.g.,Sfor sleeping,Rfor running).START: Start time of the process.TIME: Cumulative CPU time used by the process.COMMAND: Command that started the process.
2. top (Task Manager)
top provides a dynamic, real-time view of the system's processes.
Syntax:
top
Sample Output:
top - 10:47:09 up 5 days, 3:22, 2 users, load average: 0.15, 0.13, 0.11
Tasks: 130 total, 1 running, 129 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.8 us, 1.1 sy, 0.0 ni, 95.0 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3921.7 total, 1286.0 free, 1227.3 used, 1408.4 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 2435.2 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
123 root 20 0 169448 6092 4120 S 1.2 0.2 0:03.12 init
1300 user 20 0 254548 12576 1048 S 0.0 0.3 0:10.34 bash
- Explanation:
- Shows the top processes by CPU and memory usage.
- %CPU: CPU usage by the process.
- %MEM: Memory usage by the process.
- TIME+: Total time the process has been running.
Press q to exit top.
3. htop (Interactive Process Viewer)
htop is a more user-friendly, interactive version of top that allows you to scroll and search processes.
Install htop (if not already installed):
sudo apt install htop # For Debian/Ubuntu
sudo yum install htop # For CentOS/RedHat
Syntax:
htop
Sample Output:
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ COMMAND
1234 user 20 0 254548 12576 1048 S 0.0 0.3 0:10 bash
5678 root 20 0 169448 6092 4120 S 1.2 0.2 0:03 init
- Explanation:
- Similar to
top, but with colorful and more customizable output. - You can scroll up/down to see all processes, search for processes, and even kill processes directly from the interface.
- Similar to
Press F10 to exit htop.
4. pgrep (Process Grep)
pgrep searches for processes based on name or other criteria.
Syntax:
pgrep [options] pattern
Example:
To find all processes with "python" in the name:
pgrep python
Sample Output:
1234
5678
- Explanation:
pgrepreturns the PID of processes matching the search term (pythonin this case).
5. kill (Terminate Process)
The kill command is used to send signals to processes, typically to terminate them.
Syntax:
kill [signal] PID
Example:
To kill a process with PID 1234:
kill 1234
To Force Kill:
If a process doesn't respond to a normal kill, use:
kill -9 1234
Sample Output:
No output on success, but if the process is killed successfully, it will stop running.
6. nice (Start a Process with Modified Scheduling Priority)
nice is used to run a command with a specified priority (niceness).
Syntax:
nice -n [priority] command
- Priority: The range is from -20 (highest priority) to 19 (lowest priority).
Example:
To run a command with lower priority:
nice -n 10 my_script.sh
7. renice (Change Process Priority)
renice allows you to change the priority of an already running process.
Syntax:
renice -n [priority] -p PID
Example:
To change the priority of PID 1234 to 10:
sudo renice -n 10 -p 1234
Sample Output:
1234 (process PID) old priority 0, new priority 10
8. pstree (Process Tree)
pstree displays processes in a tree-like format, showing the parent-child relationships between processes.
Syntax:
pstree
Example:
pstree
Sample Output:
init─┬─systemd─┬─sshd───bash─┬─vim
│ └─sshd───bash───pstree
- Explanation:
- Shows a tree of processes, starting from the
initprocess at the top. - Processes are grouped by their parent-child relationship.
- Shows a tree of processes, starting from the
9. time (Measure Execution Time)
The time command is used to measure the execution time of a command.
Syntax:
time command
Example:
time ls
Sample Output:
real 0m0.004s
user 0m0.001s
sys 0m0.002s
- Explanation:
real: Actual elapsed time.user: Time spent in user mode.sys: Time spent in kernel mode.
10. watch (Run Command Repeatedly)
The watch command is useful for monitoring changes in real time, such as process updates.
Syntax:
watch [options] command
Example:
To monitor CPU and memory usage every 2 seconds:
watch -n 2 "ps aux"
Sample Output:
Every 2.0s: ps aux Tue Nov 12 10:50:00 2019
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 169448 6092 ? Ss 10:15 0:03 /sbin/init