Skip to content

Practice Exercise: Listing Processes with ps and top

Objectives

  • Learn how to list and monitor running processes using ps and top commands.
  • Gain practical experience in process management on a Linux system.

Scenario

Understanding how to list and monitor processes is crucial for Linux system administrators. In this practice exercise, you will explore the ps and top commands, which provide insights into the processes running on a Linux system.

Before you begin...

Make sure that you have read the Lab Environment before starting the exercise.

Tasks

Task 1: Using ps to List Processes

  • Open a terminal on your Linux system.
  • Execute the ps command to list the running processes.
  • Observe the default output format, which includes process IDs (PIDs), terminal IDs (TIDs), and process names.
  • Explore various options and arguments of the ps command to customize the process listing (e.g., ps aux or ps -ef for a more detailed list).
  • What are the three options that ps accepts?
[intern@intern-a1t-inf-lnx1 ~]$ ps
  PID TTY           TIME CMD
    PID TTY          TIME CMD
  22462 pts/1    00:00:00 bash
  22503 pts/1    00:00:00 ps

[intern@intern-a1t-inf-lnx1 ~]$ ps aux
[intern@intern-a1t-inf-lnx1 ~]$ ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  1.2 172588 12392 ?        Ss   Oct11   0:05 /sbin/init
root           2  0.0  0.0      0     0 ?        S    Oct11   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   Oct11   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   Oct11   0:00 [rcu_par_gp]
root           5  0.0  0.0      0     0 ?        I<   Oct11   0:00 [slub_flushwq]
root           6  0.0  0.0      0     0 ?        I<   Oct11   0:00 [netns]
root           8  0.0  0.0      0     0 ?        I<   Oct11   0:00 [kworker/0:0H-events_highpri]
root          10  0.0  0.0      0     0 ?        I<   Oct11   0:00 [mm_percpu_wq]
root          12  0.0  0.0      0     0 ?        I    Oct11   0:00 [rcu_tasks_kthread]
...
...
...
intern       22462  0.0  0.3 224920  3840 pts/1    Ss   06:30   0:00 -bash
root       22496  0.0  0.0      0     0 ?        R    06:30   0:00 [kworker/u2:0-events_unbound]
root       22501  0.0  0.0      0     0 ?        I    06:40   0:00 [kworker/0:2-cgroup_pidlist_destroy]
root       22502  0.0  0.0      0     0 ?        I    06:40   0:00 [kworker/0:3-events]
intern       22506  0.0  0.3 257436  3840 pts/1    R+   06:45   0:00 ps aux
[intern@intern-a1t-inf-lnx1 ~]$ ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 Oct11 ?        00:00:05 /sbin/init
root           2       0  0 Oct11 ?        00:00:00 [kthreadd]
root           3       2  0 Oct11 ?        00:00:00 [rcu_gp]
root           4       2  0 Oct11 ?        00:00:00 [rcu_par_gp]
root           5       2  0 Oct11 ?        00:00:00 [slub_flushwq]
root           6       2  0 Oct11 ?        00:00:00 [netns]
root           8       2  0 Oct11 ?        00:00:00 [kworker/0:0H-events_highpri]
root          10       2  0 Oct11 ?        00:00:00 [mm_percpu_wq]
...
...
intern       22455   22452  0 06:30 ?        00:00:00 (sd-pam)
intern       22461   22444  0 06:30 ?        00:00:00 sshd: intern@pts/1
intern       22462   22461  0 06:30 pts/1    00:00:00 -bash
root       22496       2  0 06:30 ?        00:00:00 [kworker/u2:0-events_unbound]
root       22502       2  0 06:40 ?        00:00:00 [kworker/0:3-mm_percpu_wq]
intern       22507   22462  0 06:46 pts/1    00:00:00 ps -ef

Task 2: Viewing Process Hierarchy

  • Use the ps command to display processes in a hierarchical tree structure.
  • What is the parameter needed to achieve this?
  • Identify the parent-child relationships between processes.
  • Note the PPID (Parent Process ID) column to understand which process spawned other processes.
[intern@intern-a1t-inf-lnx1 ~]$ pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─2*[agetty]
        ├─atd
        ├─auditd───{auditd}
        ├─crond
        ├─dbus-daemon
        ├─node_exporter───3*[{node_exporter}]
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sleep
        ├─splunkd─┬─splunkd
        │         └─43*[{splunkd}]
        ├─sshd───sshd───sshd───bash───pstree
        ├─2*[systemd───(sd-pam)]
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        └─tuned───5*[{tuned}]


[intern@intern-a1t-inf-lnx1 ~]$ ps aux --forest
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           2  0.0  0.0      0     0 ?        S    Oct11   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   Oct11   0:00  \_ [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   Oct11   0:00  \_ [rcu_par_gp]
root           5  0.0  0.0      0     0 ?        I<   Oct11   0:00  \_ [slub_flushwq]
root           6  0.0  0.0      0     0 ?        I<   Oct11   0:00  \_ [netns]
root           8  0.0  0.0      0     0 ?        I<   Oct11   0:00  \_ [kworker/0:0H-events_highpri]
root          10  0.0  0.0      0     0 ?        I<   Oct11   0:00  \_ [mm_percpu_wq]
root          12  0.0  0.0      0     0 ?        I    Oct11   0:00  \_ [rcu_tasks_kthread]
root          13  0.0  0.0      0     0 ?        I    Oct11   0:00  \_ [rcu_tasks_rude_kthread]
root          14  0.0  0.0      0     0 ?        I    Oct11   0:00  \_ [rcu_tasks_trace_kthread]
root          15  0.0  0.0      0     0 ?        S    Oct11   0:00  \_ [ksoftirqd/0]
root          16  0.0  0.0      0     0 ?        I    Oct11   0:01  \_ [rcu_preempt]
...
...
...
root       20370  0.0 13.9 311256 135576 ?       Ssl  Oct12   1:11 splunkd --under-systemd --systemd-delegate=yes -p 8089 _internal_launch_und
root       20404  0.0  2.1 133352 21272 ?        Ss   Oct12   0:00  \_ [splunkd pid=20370] splunkd --under-systemd --systemd-delegate=yes -p 8
intern       22452  0.0  1.1  89464 11116 ?        Ss   06:30   0:00 /usr/lib/systemd/systemd --user
intern       22455  0.0  0.8 232196  8124 ?        S    06:30   0:00  \_ (sd-pam)

Task 3: Monitoring Processes with top

  • Launch the top command in your terminal.
  • Observe the dynamic real-time display of system statistics and process information.
  • Pay attention to CPU usage, memory usage, load averages, and the list of running processes.
  • Use the q key to exit top when you're done.
[intern@intern-a1t-inf-lnx1 ~]$ top
top - 03:19:22 up 5 days,  1:45,  1 user,  load average: 0.00, 0.00, 0.00
Tasks: 160 total,   1 running, 159 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.3 si,  0.0 st
MiB Mem :    946.4 total,    166.9 free,    279.4 used,    500.0 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.    472.1 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      1 root      20   0  172588  10416   7768 S   0.0   1.1   0:06.98 systemd
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.04 kthreadd
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp
      4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp
      5 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 slub_flushwq
      6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 netns
      8 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-events_highpri

Task 4: Sorting and Filtering Processes in top

  • Start top again.
  • Experiment with sorting options by pressing keys like P, M, and T.
  • Use the o key to apply filters. For example, press o and enter a filter like USER eq <username> to display processes for a specific user.
  • Explore other sorting and filtering options available in top.
[intern@intern-a1t-inf-lnx1 ~]$ top

Task 5: Sending Signals to Processes

  • Identify a process from the ps or top list that you want to interact with.
  • Note its PID (Process ID).
  • Use the kill command to send a signal to the selected process (e.g., kill <PID> to gracefully terminate it).
  • Observe how the process responds to different signals.

# Run sleep in the background
[intern@intern-a1t-inf-lnx1 ~]$ sleep 1000000 &

[intern@intern-a1t-inf-lnx1 ~]$ ps aux | grep sleep
intern       22512  0.0  0.1 217088  1792 pts/1    S    06:49   0:00 sleep 1000000
intern       22513  0.0  0.2 221944  2304 pts/1    R+   06:48   0:00 grep --color=auto sleep

# Kill the sleep process forcefully using the SIGKILL (-9) signal
[intern@intern-a1t-inf-lnx1 ~]$ kill -9 22512
[intern@intern-a1t-inf-lnx1 ~]$
[1]+  Killed              sleep 1000000

Conclusion

This practice exercise has provided you with practical experience in listing and monitoring processes using the ps and top commands on a Linux system. You've learned how to customize process listings, view process hierarchies, monitor system statistics, sort and filter processes, and send signals to control processes. These skills are essential for system administrators to efficiently manage processes and troubleshoot system performance.