- Posted on
- • Advanced
Bash one-liners for system profiling
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Mastering Linux System Profiling with Bash One-Liners
For system administrators and developers, having a toolbox equipped with the right commands can make data gathering about system health and performance both thorough and efficient. Linux, with its robust set of utilities, provides an excellent platform for detailed system profiling.
In this post, we’re going to look at some powerful Bash one-liners that can help you quickly profile a Linux system. We will also cover the installation of necessary packages using different Linux package managers such as apt
(for Debian-based systems), dnf
(for Fedora and RHEL-based systems), and zypper
(for openSUSE).
CPU and Memory Utilization
A basic yet crucial aspect of system profiling is monitoring CPU and memory usage. Here’s how you can quickly check it using the top
command:
top -b -n 1 | head -5
This command runs top
in batch mode (-b
) to prevent it from running interactively and refreshes just once (-n 1
), then pipes the first five lines (head -5
) that contain your system’s load average and CPU/Memory usage.
Installing htop
for Enhanced System Monitoring
For a more detailed view, htop
provides an interactive monitor that visualizes processes’ resource usage more clearly than top
. To install htop
:
Debian/Ubuntu:
sudo apt install htop
Fedora:
sudo dnf install htop
openSUSE:
sudo zypper install htop
Once installed, run it by simply typing:
htop
Disk Usage
To check the disk usage by directories sorted by size you can use:
du -sh /* | sort -rh | head -10
This displays the size of each directory within the root directory in a human-readable format (-sh
), sorts them (sort -rh
) in human-readable (-h
) reverse (-r
) order, and then shows the top 10.
Network Connections and Listening Ports
Checking active network connections and listening ports can be critical, particularly for a system exposed to the internet. Use:
netstat -tuln
However, netstat
might not be installed by default. Let's install net-tools
:
Debian/Ubuntu:
sudo apt install net-tools
Fedora:
sudo dnf install net-tools
openSUSE:
sudo zypper install net-tools
Real-time Monitoring with vmstat
For real-time system performance analysis, vmstat
is invaluable. Install it and execute it to view report on system processes, memory, paging, block I/O, traps, and CPU activity.
vmstat 1 5
This command means vmstat
runs every second (1
) for five seconds (5
).
System Uptime
Checking how long the system has been running is as simple as:
uptime
Conclusion
These Bash one-liners provide a quick snapshot of your system’s health and are handy for both swift checks and deep dives into how the operating system is performing. Remember, while one-liners are powerful, they can also be destructive if used improperly. Always ensure you understand a command before running it, especially as the superuser.
Happy profiling, and may your systems run smoothly and swiftly!
Further Reading
For those interested in expanding their knowledge about system profiling and monitoring using Bash and command-line tools, the following resource articles could be of great benefit:
Advanced Bash-Scripting Guide: An in-depth exploration of scripting in Bash to automate tasks and improve system management. https://tldp.org/LDP/abs/html/
Understanding the
top
Command: A detailed guide to mastering thetop
command for monitoring system processes and performance. https://www.howtogeek.com/107217/Linux Performance: Provides a comprehensive look into system performance tools available on Linux, with practical examples. https://www.brendangregg.com/linuxperf.html
Netstat – A Network Monitoring Tool: Learn about
netstat
for monitoring network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. https://linuxize.com/post/netstat-command-in-linux/Using
vmstat
to Monitor System Performance: A simple tutorial on utilizingvmstat
for real-time system performance analysis and troubleshooting. https://www.tecmint.com/install-vmstat-to-report-virtual-memory-statistics/
Each link provides practical insights and deeper knowledge on utilizing command-line tools for effective Linux system administration and profiling.