Practice Exercise: Process Metrics and Process Control
Objectives
- Explore process metrics and resource utilization.
- Learn how to control processes and their execution.
- Gain practical experience with process management in a Linux environment.
Scenario
Understanding how processes work and how to manage them efficiently is essential for Linux system administrators. This practice exercise will provide hands-on experience with process metrics, resource monitoring, and process control.
Before you begin...
Make sure that you have read the Lab Environment before starting the exercise.
Tasks
Task 1: Monitoring Process Metrics
- Open a terminal on your Linux system.
- Use the
ps
command to display a list of running processes. - Pay attention to the columns displaying process IDs (PIDs), CPU usage, memory usage, and execution status.
- Identify a process with high CPU or memory usage.
- Run
ps
with the option to sort the process from the highest CPU usage to the lowest - Run
ps
with the option to sort the process from the highest Memory usage to the lowest
[intern@intern-a1t-inf-lnx1 ~]$ ps aux --sort=-%cpu
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 616 1.3 4.4 1429108 90400 ? Ssl 14:36 0:15 /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --config=/var/lib/kubelet/config.yaml --container-runtime-endpoint=/var/run/cri-dockerd.sock --pod-infra-container-image=registry.k8s.io/pause:3.9
root 711 0.5 3.1 1363056 63752 ? Ssl 14:36 0:06 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 910 0.5 1.9 1185632 38928 ? Ssl 14:36 0:06 /usr/local/bin/cri-dockerd --container-runtime-endpoint fd://
root 640 0.3 1.8 1113832 36520 ? Ssl 14:36 0:03 /usr/bin/containerd
root 2121 0.2 1.3 1244544 28116 ? Ssl 14:41 0:01 /usr/lib/snapd/snapd
[intern@intern-a1t-inf-lnx1 ~]$ ps aux --sort=-%mem
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 616 1.3 4.4 1429108 90400 ? Ssl 14:36 0:17 /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --config=/var/lib/kubelet/config.yaml --container-runtime-endpoint=/var/run/cri-dockerd.sock --pod-infra-container-image=registry.k8s.io/pause:3.9
root 711 0.5 3.1 1363056 62752 ? Ssl 14:36 0:07 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 910 0.5 1.9 1185632 38928 ? Ssl 14:36 0:06 /usr/local/bin/cri-dockerd --container-runtime-endpoint fd://
root 640 0.3 1.8 1113832 36520 ? Ssl 14:36 0:04 /usr/bin/containerd
root 2121 0.2 1.3 1244544 28008 ? Ssl 14:41 0:01 /usr/lib/snapd/snapd
Task 2: Gathering Detailed Process Information
- Choose a process from the list generated in Task 1 and note its PID.
- Use the
pidstat
command followed by the PID to gather detailed statistics about the selected process (e.g.,pidstat -p <PID>
). - Observe metrics such as CPU usage, memory usage, and I/O statistics in real-time.
[intern@intern-a1t-inf-lnx1 ~]$ pidstat -p 310
Linux 6.2.9-x86_64-linode160 (intern-a1t-inf-lnx1) 10/03/2023 _x86_64_ (1 CPU)
05:56:01 AM UID PID %usr %system %guest %wait %CPU CPU Command
05:56:01 AM 0 310 0.00 0.00 0.00 0.00 0.00 0 sshd
Task 3: Adjusting Process Priority
- Use the
nice
command to adjust the process's priority level (e.g.,nice -n 19 command
). - Monitor the process's behavior and resource utilization after changing its priority.
- By running the command using the default niceness
[intern@intern-a1t-inf-lnx1 ~]$ tar -cvzf applications.tar /usr/ > /dev/null 2>&1 &
- And by running
top
in another tab you can see its nicenessNI
set to 0PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 13339 intern 20 0 3372 1812 1228 R 93.3 0.2 0:25.59 gzip 13338 intern 20 0 7440 3304 2780 S 4.0 0.3 0:01.49 tar
- By running the command using a user defined niceness
[intern@intern-a1t-inf-lnx1 ~]$ nice -n 19 tar -cvzf applications.tar /usr/ > /dev/null 2>&1 &
- Run the
top
again and see its niceness being changedPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 13378 intern 39 19 3372 1824 1240 R 96.7 0.2 0:04.07 gzip 13377 intern 39 19 7148 3004 2788 S 2.0 0.3 0:00.09 tar
Task 4: Controlling Process Execution
- Launch a sample command that runs indefinitely in the terminal (e.g.,
while :; do echo "Running..."; sleep 1; done
). - Suspend the running command by pressing
Ctrl + Z
. - Use the
bg
command to send the suspended process to the background. - Use the
jobs
command to view background jobs. - Bring the background process to the foreground using the
fg
command.
[intern@intern-a1t-inf-lnx1 ~]$ while :; do echo "Running..."; sleep 1; done
Running...
# Press ctrl + z
^Z[1] + 24406 suspended sleep 1
[intern@intern-a1t-inf-lnx1 ~]$ bg
[1] + 24406 continued sleep 1
Running...
# After pressing bg the stdout will be in your terminal
[intern@intern-a1t-inf-lnx1 ~]$ fg
[1] + 24409 running while :; do; echo "Running..."; sleep 1; done
Task 5: Sending Signals to Processes
- Identify a process from the task 4 and note its PID.
- Use the
kill
command to send different signals to the selected process (e.g.,kill -9 <PID>
to forcefully terminate it). - Observe the effects of different signals on the process.
[intern@intern-a1t-inf-lnx1 ~]$ kill -9 24406
[1] + 24407 killed while :; do; echo "Running..."; sleep 1; done
[intern@intern-a1t-inf-lnx1 ~]$ kill -9 24406
[1] + 24407 killed while :; do; echo "Running..."; sleep 1; done
Conclusion
This practice exercise has provided you with practical experience in working with process metrics, resource utilization, and process control on a Linux system. You've learned how to monitor running processes, gather detailed process information, adjust process priorities, control process execution, and send signals to processes. These skills are valuable for optimizing system performance and managing processes effectively.