Bookmark this page

Lab: Monitoring and Managing Linux Processes

Performance Checklist

In this lab, you will locate and manage processes that are using the most resources on a system.

Outcomes

You should be able to manage processes using top as a process management tool.

Log in to workstation as student using student as the password.

On workstation, run the lab processes-review start command. The command runs a start script to determine whether the host, serverb, is reachable on the network.

[student@workstation ~]$ lab processes-review start
  1. On workstation, open two terminal windows side by side. In this section, these terminals are referred to as left and right. On each terminal window, log in to serverb as the student user.

    Create a script called process101, which will generate artificial CPU load. Create the script in the /home/student/bin directory.

    #!/bin/bash
    while true; do
      var=1
      while [[ var -lt 50000 ]]; do
        var=$(($var+1))
      done
      sleep 1
    done
    1. On workstation, open two terminal windows side by side. In each terminal, use the ssh command to log in to serverb as the student user.

      [student@workstation ~]$ ssh student@serverb
      ...output omitted...
      [student@serverb ~]$ 
    2. In the left terminal shell, use the mkdir command to create the /home/student/bin directory.

      [student@serverb ~]$ mkdir /home/student/bin
    3. In the left terminal shell, use the vim command to create the process101 script. Press the i key to enter interactive mode. Type :wq to save the file.

      [student@serverb ~]$ vim /home/student/bin/process101
      #!/bin/bash
      while true; do
        var=1
        while [[ var -lt 50000 ]]; do
          var=$(($var+1))
        done
        sleep 1
      done
    4. Use the chmod command to make the process101 file executable.

      [student@serverb ~]$ chmod +x /home/student/bin/process101
  2. In the right window, run the top utility.

    1. In the right window, run the top utility. Size the window to be as tall as possible.

      [student@serverb ~]$ top
      top - 13:47:06 up 19 min,  2 users,  load average: 0.00, 0.00, 0.00
      Tasks: 110 total,   1 running, 109 sleeping,   0 stopped,   0 zombie
      %Cpu(s):  0.0 us,  3.1 sy,  0.0 ni, 96.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
      MiB Mem :   1829.4 total,   1439.1 free,    171.9 used,    218.4 buff/cache
      MiB Swap:   1024.0 total,   1024.0 free,      0.0 used.   1499.6 avail Mem
      
      PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME  COMMAND
        1 root      20   0  178536  13488   8996 S   0.0   0.7   0:01.15 systemd
        2 root      20   0       0      0      0 S   0.0   0.0   0:00.00 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
        6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-kblockd
      ...output omitted...
  3. In the left terminal shell, determine the number of logical CPUs on the virtual machine. Run the process101 script in the background.

    1. Use the grep command to determine the number of logical CPUs.

      [student@serverb ~]$ grep "model name" /proc/cpuinfo | wc -l
      2
    2. Use the cd command to change into the /home/student/bin directory. Run the process101 script in the background.

      [student@serverb ~]$ cd /home/student/bin
      [student@serverb bin]$ process101 &
      [1] 1180
  4. In the right terminal shell, observe the top display. Toggle between load, threads and memory. Note the process ID (PID) for process101. View the CPU percentage. It should hover around 10% to 15%. Ensure that top is showing CPU usage once you have viewed load, threads, and memory.

    1. Press Shift+m.

      top - 13:56:24 up 28 min,  2 users,  load average: 0.21, 0.08, 0.02
      Tasks: 112 total,   2 running, 110 sleeping,   0 stopped,   0 zombie
      %Cpu(s):  5.8 us,  1.3 sy,  0.0 ni, 92.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
      MiB Mem :   1829.4 total,   1438.1 free,    172.7 used,    218.6 buff/cache
      MiB Swap:   1024.0 total,   1024.0 free,      0.0 used.   1498.7 avail Mem
      
       PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
       705 root      20   0  409956  34880  33620 S   0.0   1.9   0:00.04 sssd_nss
       706 root      20   0  454304  34472  14304 S   0.0   1.8   0:00.62 firewalld
       725 root      20   0  611348  28244  14076 S   0.0   1.5   0:00.27 tuned
       663 polkitd   20   0 1907312  23876  16040 S   0.0   1.3   0:00.04 polkitd
       718 root      20   0  600316  17176  14832 S   0.0   0.9   0:00.06 NetworkManager
      ...output omitted...

      Note

      Note that when top is switched into memory mode, process101 is no longer the first process. You can press Shift+p to return to CPU usage.

    2. Press m.

      top - 09:32:52 up 20:05,  2 users,  load average: 0.18, 0.10, 0.03
      Tasks: 112 total,   2 running, 110 sleeping,   0 stopped,   0 zombie
      %Cpu(s):   7.8/1.5     9[|||||||||                                    ]
      MiB Mem : 18.3/1829.4   [|||||||||||||||||||                        ]
      MiB Swap:  0.0/1024.0   [                                           ]
       PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
       705 root      20   0  409956  34880  33620 S   0.0   1.9   0:00.04 sssd_nss
       706 root      20   0  454304  34472  14304 S   0.0   1.8   0:00.62 firewalld
       725 root      20   0  611348  28244  14076 S   0.0   1.5   0:00.30 tuned
       663 polkitd   20   0 1907312  23876  16040 S   0.0   1.3   0:00.04 polkitd
       718 root      20   0  600316  17176  14832 S   0.0   0.9   0:00.07 NetworkManager
      ...output omitted...
    3. Press t.

      Tasks: 113 total,   2 running, 111 sleeping,   0 stopped,   0 zombie
      %Cpu(s):   7.8/1.5     9[|||||||||                                           ]
      MiB Mem :   1829.4 total,   1436.7 free,    173.7 used,    219.0 buff/cache
      MiB Swap:   1024.0 total,   1024.0 free,      0.0 used.   1497.7 avail Mem
      
       PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      1180 student   20   0  222448   3056   2808 S  12.0   0.2   1:59.94 process101
       705 root      20   0  409956  34880  33620 S   0.0   1.9   0:00.04 sssd_nss
       706 root      20   0  454304  34472  14304 S   0.0   1.8   0:00.62 firewalld
       725 root      20   0  611348  28244  14076 S   0.0   1.5   0:00.30 tuned
       663 polkitd   20   0 1907312  23876  16040 S   0.0   1.3   0:00.04 polkitd
       718 root      20   0  600316  17176  14832 S   0.0   0.9   0:00.07 NetworkManager
      ...output omitted...
    4. Press Shift+p.

      top - 09:35:48 up 20:08,  2 users,  load average: 0.10, 0.10, 0.04
      Tasks: 110 total,   4 running, 106 sleeping,   0 stopped,   0 zombie
      %Cpu(s):  6.8 us,  1.0 sy,  0.0 ni, 92.2 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
      MiB Mem : 18.3/1829.4   [|||||||||||||||||||                                  ]
      MiB Swap:  0.0/1024.0   [                                                     ]
      
        PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      27179 student   20   0  222448   3060   2812 R  15.6   0.2   1:13.47 process101
      ...output omitted...
  5. Turn off the use of bold in the display. Save this configuration for reuse when top is restarted. Confirm that the changes are saved.

    1. Press Shift+b to switch the use of bold off.

      top - 19:40:30 up  6:12,  2 users,  load average: 0.11, 0.12, 0.09
      Tasks: 112 total,   1 running, 111 sleeping,   0 stopped,   0 zombie
      %Cpu(s):   7.6/1.5     9[|||||||||                                     ]
      MiB Mem : 18.2/1829.4   [|||||||||||||||||||                           ]
      MiB Swap:  0.0/1024.0   [                                              ]
    2. Press Shift+w to save this configuration. The default configuration is stored in toprc beneath the /home/student/.config/procps directory. In the left terminal shell, confirm that the toprc file exists.

      [student@serverb bin]$ ls -l /home/student/.config/procps/toprc
      -rw-rw-r--. 1 student student 966 Feb 18 19:45 /home/student/.config/procps/toprc 
    3. In the right terminal shell, exit top, and then restart it. Confirm that the new display uses the saved configuration.

      top - 00:58:21 up 43 min,  2 users,  load average: 0.29, 0.28, 0.20
      Tasks: 105 total,   1 running, 104 sleeping,   0 stopped,   0 zombie
      %Cpu(s):  11.0/1.8    13[|||||||||||||                                 ]
      MiB Mem : 18.7/1829.0   [|||||||||||||||||||                           ]
      MiB Swap:  0.0/0.0      [                                              ]
  6. Copy the process101 script to a new file called process102. Edit the script to create more artificial CPU load. Increase the load from fifty thousand to one hundred thousand. Start the process102 process in the background.

    1. In the left terminal shell, use the cp command to copy process101 to process102.

      [student@serverb bin]$ cp process101 process102
    2. Use the vim command to edit the process102 script. Increase the addition problems from fifty thousand to one hundred thousand. Enter interactive mode by using the i. Type :wq to save the file.

      [student@serverb bin]$ vim process102
      #!/bin/bash
      while true; do
        var=1
          while [[ var -lt 100000 ]]; do
            var=$(($var+1))
        done
        sleep 1
      done
    3. Start the process102 process in the background.

      [student@serverb bin]$ process102 &
      [2] 20723 
    4. Use the jobs command to confirm that both processes are running in the background.

      [student@serverb bin]$ jobs
      [1]-  Running                 process101 &
      [2]+  Running                 process102 & 
  7. In the right terminal shell, confirm that the process is running and using the most CPU resources. The load should be hovering between 25% and 35%.

    1. In the right terminal shell, confirm that the process is running and using the most CPU resources. The load should be hovering between 25% and 35%.

      top - 20:14:16 up  6:46,  2 users,  load average: 0.58, 0.34, 0.18
      Tasks: 112 total,   2 running, 110 sleeping,   0 stopped,   0 zombie
      499 %Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
      500 MiB Mem :   1829.4 total,   1428.7 free,    179.0 used,    221.8 buff/cache
      501 MiB Swap:   1024.0 total,   1024.0 free,      0.0 used.   1492.1 avail Mem
      
       PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      20723 student   20   0  222448   3016   2764 S  24.7   0.2   0:53.28 process102
       1180 student   20   0  222448   3056   2808 S  12.0   0.2  58:01.56 process101
      ...output omitted...

      Note

      If you do not see process101 and process102 at the top of the process list, press Shift+p to ensure that top is sorted by CPU utilization.

  8. The load average is still below 1. Copy process101 to a new script called process103. Increase the addition count to eight hundred thousand. Start process103 in the background. Confirm that the load average is above 1. It may take a few minutes for the load average to change.

    1. In the right terminal shell, confirm that the load average is below 1.

      top - 20:24:13 up  6:56,  2 users,  load average: 0.43, 0.41, 0.29
      ...output omitted...
    2. In the left terminal shell, use the cp command to copy process101 to a new script called process103.

      [student@serverb bin]$ cp process101 process103
    3. In the left terminal shell, use the vim command to edit the process103 script. Increase the addition count to eight hundred thousand. Enter interactive mode using the i key. Type :wq to save the file.

      [student@serverb bin]$ vim process103
      #!/bin/bash
      while true; do
        var=1
          while [[ var -lt 800000 ]]; do
            var=$(($var+1))
          done
          sleep 1
      done
    4. Start process103 in the background. The CPU usage hovers between 60% and 85%.

      [student@serverb bin]$ process103 &
      [3] 22751 
    5. Confirm that all three jobs are running in the background.

      [student@serverb bin]$ jobs
      [1]   Running                 process101 &
      [2]-  Running                 process102 &
      [3]+  Running                 process103 & 
    6. In the right terminal window confirm that the load average is above 1.

      top - 20:45:34 up  7:17,  2 users,  load average: 1.10, 0.90, 0.64
  9. In the left terminal shell, become root. Suspend the process101 process. List the remaining jobs. Observe that the process state for process101 is now T.

    1. Use the su - command to become root. The password is redhat.

      [student@serverb bin]$ su -
      Password: redhat
    2. Use the pkill command with the -SIGSTOP option to suspend the process101 process.

      [root@serverb ~]# pkill -SIGSTOP process101
    3. In the right terminal shell confirm that process101 is no longer running.

      top - 20:52:01 up  7:24,  2 users,  load average: 1.19, 1.19, 0.89
      Tasks: 112 total,   2 running, 110 sleeping,   0 stopped,   0 zombie
      499 %Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
      500 MiB Mem :   1829.4 total,   1428.7 free,    179.0 used,    221.8 buff/cache
      501 MiB Swap:   1024.0 total,   1024.0 free,      0.0 used.   1492.1 avail Mem
      
       PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      24043 student   20   0  222448   2992   2744 R  66.1   0.2   6:59.50 process103
      20723 student   20   0  222448   3016   2764 R  29.9   0.2  11:04.84 process102
      ...output omitted...
    4. In the left terminal shell run the ps jT command to view the remaining jobs.

      [root@serverb ~]# ps jT
       PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
       ...output omitted...
       27138  1180  1180 27138 pts/0    28558 T     1000   3:06 /bin/bash /home/student/bin/process101
       27138 20723 20723 27138 pts/0    28558 R     1000   1:23 /bin/bash /home/student/bin/process102
       27138 24043 24043 27138 pts/0    28558 R     1000   2:35 /bin/bash /home/student/bin/process103
       ...output omitted...

      Note that process101 has a status of T. This denotes that the process is currently suspended.

  10. Resume the process101 process.

    1. In the left terminal shell use the pkill command with the -SIGCONT option to resume the process101 process.

      [root@serverb ~]# pkill -SIGCONT process101
    2. In the right terminal shell confirm that the process is running again.

      top - 20:57:02 up  7:29,  2 users,  load average: 1.14, 1.20, 0.99
      Tasks: 112 total,   2 running, 110 sleeping,   0 stopped,   0 zombie
      %Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
      MiB Mem :   1829.4 total,   1428.7 free,    179.0 used,    221.8 buff/cache
      MiB Swap:   1024.0 total,   1024.0 free,      0.0 used.   1492.1 avail Mem
      
       PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      24043 student   20   0  222448   2992   2744 R  66.8   0.2  10:40.61 process103
      20723 student   20   0  222448   3016   2764 S  24.9   0.2  12:25.10 process102
       1180 student   20   0  222448   3056   2808 S  17.9   0.2  64:07.99 process101 
  11. Terminate process101, process102, and process103 using the command line. Confirm that the processes are no longer displayed in top.

    1. In the left terminal shell, use the pkill command terminate process101, process102, and process103.

      [root@serverb ~]# pkill process101
      [root@serverb ~]# pkill process102
      [root@serverb ~]# pkill process103
    2. In the right terminal shell, confirm that the processes no longer appear in top.

      top - 21:05:06 up  7:37,  2 users,  load average: 1.26, 1.29, 1.12
      Tasks: 112 total,   2 running, 110 sleeping,   0 stopped,   0 zombie
      499 %Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
      500 MiB Mem :   1829.4 total,   1428.7 free,    179.0 used,    221.8 buff/cache
      501 MiB Swap:   1024.0 total,   1024.0 free,      0.0 used.   1492.1 avail Mem
      
      PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
        1 root      20   0  178536  13488   8996 S   0.0   0.7   0:01.21 systemd
        2 root      20   0       0      0      0 S   0.0   0.0   0:00.00 kthreadd
        3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp
      ...output omitted...
  12. In the left terminal shell, exit from the root user. In the right terminal shell stop the top command. Exit from serverb in both windows.

    1. Use the exit command to log out from the root user.

      [root@serverb ~]# exit
      logout
      [1]   Terminated              process101
      [2]   Terminated              process102
      [3]-  Terminated              process103
    2. Exit from all terminal windows.

      [student@serverb bin]$ exit
      [student@workstation ~]$ 
    3. In the right terminal shell, press q to quit top. Use the exit command to log out.

      [student@serverb ~]$ exit
      logout
      Connection to serverb closed.
      [student@workstation ~]$ 

Evaluation

On workstation, run the lab processes-review grade script to confirm success on this exercise.

[student@workstation ~]$ lab processes-review grade

Finish

On workstation, run the lab processes-review finish script to complete the lab.

[student@workstation ~]$ lab processes-review finish

This concludes the lab.

Revision: rh124-8.2-df5a585