Introduction
In this challenge, you will enhance the provided Linux system monitor script by adding a logging feature. The goal is to record resource usage (CPU, memory, and disk) into a log file after every monitoring cycle. This task will test your ability to modify and extend a shell script to include file-based output.
The base script is provided for you. You need to add the logging feature to it.
Add Logging to the System Monitor
Tasks
- Modify the script
/home/labex/project/system_monitor.shto append the resource usage (CPU, memory, and disk) to a log file namedresource_usage.login the same directory. Each entry should include the current date and time.
Requirements
- The log file must be named
resource_usage.logand located in/home/labex/project. - Each log entry must include the date, time, and resource usage percentages for CPU, memory, and disk.
- The logging must be implemented after the resource usage values are calculated in each monitoring cycle.
Hints
- Use the
datecommand to get the current date and time, such asdate '+%Y-%m-%d %H:%M:%S'. Explore theman datepage to learn how to format the output. - Remember to use the
>>operator to append content to a file. Avoid overwriting the file each time. - Combine the resource usage values into a single string before appending to the log file. Use variables and string interpolation for this purpose.
- Ensure the log file is written in the same directory as the script, using an absolute or relative path like
/home/labex/project/resource_usage.log.
Examples
After completing the task, running /home/labex/project/system_monitor.sh for a few seconds should produce a resource_usage.log file with entries similar to:
cat /home/labex/project/resource_usage.log
2024-12-04 14:00:01 CPU: 15% Memory: 45% Disk: 62%
2024-12-04 14:00:03 CPU: 18% Memory: 46% Disk: 62%
2024-12-04 14:00:05 CPU: 20% Memory: 47% Disk: 62%
To exit the script gracefully, you can press Ctrl+C to send an interrupt signal.

Summary
In this challenge, you added a logging feature to the Linux system monitor script. This feature records resource usage into a log file, allowing users to track resource consumption over time. This task reinforced your understanding of file operations and date commands in Bash scripting.



