Listing Running Processes
Listing running processes is a fundamental task in Linux system administration and troubleshooting. Linux provides several commands and options to list running processes, each with its own set of features and use cases.
The ps
Command
The ps
(process status) command is the most commonly used tool for listing running processes. It provides a snapshot of the current state of running processes on the system.
Here's an example of using the ps
command to list all running processes:
$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:30 ? 00:00:03 /sbin/init
root 2 0 0 10:30 ? 00:00:00 [kthreadd]
root 3 2 0 10:30 ? 00:00:00 [rcu_gp]
root 4 2 0 10:30 ? 00:00:00 [rcu_par_gp]
root 6 2 0 10:30 ? 00:00:00 [kworker/0:0H-kblockd]
The ps -ef
command displays the full process listing, including the user ID (UID), process ID (PID), parent process ID (PPID), CPU utilization (C), start time (STIME), terminal (TTY), CPU time (TIME), and the command (CMD) that started the process.
The top
Command
The top
command provides a real-time, dynamic view of the running processes on the system. It displays information such as CPU and memory usage, as well as the list of running processes sorted by various criteria.
Here's an example of the top
command in action:
$ top
top - 10:35:42 up 1 day, 23:05, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 167 total, 1 running, 166 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2047820 total, 172604 free, 416640 used, 1458576 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 997864 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 4424 804 736 S 0.0 0.0 0:03.11 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd
3 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp
6 root 20 0 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H-kblockd
The top
command provides a real-time view of system resource utilization and the list of running processes, allowing you to monitor and troubleshoot your system effectively.
The pgrep
Command
The pgrep
command is a powerful tool for finding processes based on various criteria, such as the process name, user, or other attributes. It can be used to quickly identify running processes without the need to parse the output of the ps
command.
Here's an example of using pgrep
to find all processes owned by the root
user:
$ pgrep -u root
1
2
3
4
6
The pgrep -u root
command will return the PIDs of all processes owned by the root
user.
By combining the power of the ps
, top
, and pgrep
commands, you can effectively list and manage running processes on your Linux system.