Chapter 8: Monitor and Manage Linux Processes
Welcome back to my ongoing blog series on RH124 (Red Hat System Administration I) certification exam preparation. In today's blog post, I will dive into Chapter 8, which covers "Monitor and Manage Linux Processes." As a Linux administrator, it is essential to have a deep understanding of the processes running on a system and how to manage them effectively. In this chapter, I have learnt how to determine the status, resource use, and ownership of running programs on a Linux system, and how to control them using various commands and techniques. One of the critical objectives of this chapter is to familiarize ourselves with Bash job control, which allows us to manage multiple processes that were started from the same terminal session. I will explore commands that enable us to kill processes, communicate with processes, define daemon processes, and stop user sessions and processes. Additionally, I will delve into the concept of load average, which helps us determine the resource-intensive processes on a Linux server. Understanding the load average can provide valuable insights into system performance and resource utilization. Throughout this chapter, I will work on a lab comprising 12 tasks, focusing on monitoring and managing Linux processes. These hands-on tasks will provide practical experience using various commands and techniques to manage processes effectively on a Linux system. So, let's dive into Chapter 8 and learn how to effectively monitor and manage Linux processes to ensure smooth system operation and performance optimization.
Question 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 the process101
script in the /home/student/bin
directory. The process101
script generates artificial CPU load.
#!/bin/bash while true; do var=1 while [[ var -lt 50000 ]]; do var=$(($var+1)) done sleep 1 done
Solution:
- I opened two terminal windows side by side on my workstation. You can do this by launching two separate terminal applications or by splitting the screen of a single terminal emulator into two windows.
- I logged in to serverb as the student user in each terminal window using the
ssh
command. This involves typing the commandssh student@serverb
in both the left and right terminals and pressing Enter, and then entering the appropriate password or key for authentication. - In the right terminal, I created the
/home/student/bin
directory using themkdir -p
****``/home/student/bin
command. This command creates a new directory namedbin
under the/home/student
directory if it doesn't already exist. The-p
**option ensures that any parent directories that may not exist are created as well. - Next, in the right terminal, I opened the
process101
file in the Vim editor using thevim
****``/home/student/bin/process101
command. This command creates a new file namedprocess101
under the/home/student/bin
directory and opens it in the Vim editor for editing. - In the Vim editor, I pasted the provided bash script into the file. The script generates artificial CPU load and consists of a
while
loop with nested loops and asleep
command. - I saved and exit the Vim editor by typing
:wq
and pressing Enter. This saves the changes made to theprocess101
**file and closes the Vim editor.
Following these steps, I have successfully created the process101
script in the /home/student/bin
directory on serverb, which will be used for simulating CPU load in further tasks of the lab.
Question 2: In the right terminal shell, run the top
utility.
Solution:
- I switched to the right terminal window where I have logged in to serverb as the student user.
- I typed the
top
command in the terminal and pressed Enter. This launched thetop
**utility, which provides real-time monitoring of system processes and resource usage. - The
top
utility displays a list of processes running on the system, sorted by various criteria such as CPU usage, memory usage, and more. You can use the arrow keys to navigate through the list and see different columns of information about each process.
By running the top
utility, I can monitor the processes running on the system, their resource usage, and other information that can help me determine the status and characteristics of running programs on the system, as well as identify resource-intensive processes.
Question 3: In the left terminal shell, verify the number of logical CPUs on the virtual machine. Run the process101
script in the background.
Solution:
In the left terminal shell, I started by verifying the number of logical CPUs on the virtual machine using the lscpu
command. This command provides information about the CPU architecture and characteristics, including the number of logical CPUs (threads) available in the system. Alternatively, I can also use the lscpu | grep
'``^CPU(s)``'
command to filter out the CPU information from the lscpu
output.
Next, I ran the process101
script in the background using the process101 &
command. The &
symbol at the end of the command allows the script to run in the background, so that I can continue using the terminal for other tasks while the script generates artificial CPU load in the background.
Question 4: In the right terminal shell, observe the top
display. Note the process ID (PID) and view the CPU percentage that the process101
process uses. The CPU percentage that the process uses should hover around 10% to 15%. Toggle the top
utility display between load, threads, and memory. Return to the CPU usage display of the top
utility.
Solution:
In the right terminal shell, I observed the top
display using the previously started top
command. The **top
utility provides real-time information about the processes running on the system, including their resource usage. I noted the Process ID (PID) of the process101
process from the top
display, which was 1157, the same output I got after running the process101 &
command in the left terminal.
I observed the CPU percentage that the process101
process uses, which should hover around 10% to 15%. The CPU percentage indicates the proportion of CPU resources that the process is currently utilizing.
Next, I toggled the top
utility display between load, threads, and memory using the corresponding keys (`lfor load,
Hfor threads, and
M**for memory) to view different aspects of system resource usage.
Finally, I returned to the CPU usage display of the
toputility to continue monitoring the CPU percentage used by the
process101` process and track its resource utilization.
Question 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.
Solution: To turn off the use of bold in the top
display, I pressed Shift+b in the right terminal shell. This will disable the use of bold font for highlighting processes in the top
display.
Next, to save this configuration for reuse when top
is restarted, I pressed Shift+w. This will write the current configuration to the default toprc
file, which is located in the /home/student/.config/procps
directory.
In the right terminal shell, I exited the top
utility by pressing q
and then restarted it using the top
command. I confirmed that the new display uses the saved configuration by checking that the bold font is turned off as per the configuration changes made earlier.
Question 6: Copy the process101
script to a new process102
file, and increase the artificial CPU load to one hundred thousand in the new script. Start the process102
process in the background.
Solution:
In the left terminal shell, I switched to the /home/student/bin
directory where the process101
script is located. Then, I ran the cp process101 process102
command to create a copy of the process101
script as process102
. This copied the content of process101
into a new file named process102
.
Next, I used the Vim
editor to open the process102
file. Inside the vim
editor, I switched to insert mode by pressing i
, and then I modified the artificial CPU load value from 50000
to 100000
. Once the change was made, I saved the file by pressing Esc
to exit insert mode, followed by :wq
and Enter to save and exit the editor.
Afterwards, I started the process102
process in the background by running the process102 &
command. This started the process102
script as a background process, allowing it to generate artificial CPU load with the increased value of 100000
as per the modification made in the process102
script.
Question 7: In the right terminal shell, verify that the process is running and uses the most CPU resources. The load should hover between 25% and 35%. Solution:
In the right terminal shell, I observed the top
display and verified that the process102
is running and utilizing the most CPU resources. The CPU load was hovering between 25% and 35%, as expected based on the increased artificial CPU load value of 100000
set in the process102
script. This indicates that the **process102
is using a significant amount of CPU resources, resulting in the observed CPU load range.
Question 8: Notice that the load average is below 1. Copy the process101
script 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.
Solution:
In the right terminal shell, I noticed that the load average was below 1 as I observed the top
utility. To create a new script called process103
, I used the cp process101 process103
command, which copied the content of process101
to process103
. Then, I used the vim process103
command to open the process103
file in the vim editor. I switched to insert mode and increased the addition count from 50000
to 800000
. After saving the changes, I used the process103 &
command to start process103
in the background.
I then observed the top
display in the right terminal and confirmed that process103
is now running and utilizing significant CPU resources. As a result, the load average gradually increased and went above 1, indicating that the system is experiencing a higher load due to the increased artificial CPU load in process103
. It may take a few minutes for the load average to change depending on the system's performance and load conditions.
Question 9: In the left terminal shell, switch to the root
user. Suspend the process101
process. List the remaining jobs. Observe that the process state for process101
is now in the T
state.
Solution:
In the left terminal shell, I switched to the root
user by using the command su -i
and providing the password for the root
user. Then, I used the command pkill -SIGSTOP process101
to send a stop signal to the process101
process, effectively suspending it. Next, I used the command ps
****``jT
to list the remaining jobs and observed that the process state for process101
has changed to T
, indicating that it is now in a stopped/suspended state.
Question 10: Resume the process101
process.
Solution:
To resume the process101
process, I used the command pkill -SIGCONT
****``process101
. This sent a continue signal to the process, allowing it to resume execution. Upon checking the right terminal, I confirmed that the process101
has resumed its execution.
Question 11: Terminate process101
, process102
, and process103
from the command line. Verify that the processes are no longer displayed in top
.
Solution:
To terminate the process101
, process102
, and process103
processes from the command line, I used the commands pkill process101
, pkill process102
, and pkill process103
**respectively. The pkill
command with no options sends a termination signal to the specified processes, causing them to terminate. Upon checking the top utility, I confirmed that the processes are no longer displayed, indicating that they have been successfully terminated.
Question 12: Stop processes and return to the workstation
machine.
Solution:
In the right terminal, I pressed the q
key to quit the top utility. In the left terminal, I entered the exit
command to exit from the root user session, and then entered exit
again to return to the workstation machine. The q
key is used to quit the top utility, and the exit
command is used to exit from a terminal session or return to the parent shell or workstation machine.
Conclusion
Understanding process management is crucial for effective system administration in Linux. The ability to monitor, control, and manipulate processes can greatly impact system performance and resource utilization. In this chapter, I covered various commands and utilities such as ps
, top
, and kill
, and demonstrated how to create and manage processes using scripts.
I also explored techniques to modify process load, suspend and resume processes, and terminate them when needed. By mastering these skills, Linux users can better optimize their system's performance, troubleshoot issues, and ensure efficient resource allocation.
I encourage readers to engage in further exploration and practice with process management in Linux. Experiment with different commands and utilities, create and run scripts to generate artificial CPU load, and observe the behaviour of processes using tools like top
. Share your experiences, challenges, and insights with us and the Linux community. Let's continue to learn and improve our skills in Linux process management for optimal system performance.