To retrieve CPU, memory, and disk usage percentages in a Linux environment, you can use the following commands:
1. CPU Usage
You can use the top command or mpstat from the sysstat package.
-
Using
top:top -b -n 1 | grep "Cpu(s)"This command runs
topin batch mode for one iteration and filters the output to show CPU usage. -
Using
mpstat:mpstat 1 1This command shows CPU usage statistics, where
1 1means to report every second for one iteration.
2. Memory Usage
You can use the free command.
free -m
This command displays memory usage in megabytes. The output includes total, used, free, shared, buffer/cache, and available memory.
3. Disk Usage
You can use the df command.
df -h
This command shows disk space usage in a human-readable format. It lists all mounted filesystems along with their usage percentages.
Example Output
- CPU: The output from
topmight look like:%Cpu(s): 5.0 us, 2.0 sy, 0.0 ni, 93.0 id - Memory: The output from
freemight look like:total used free shared buff/cache available Mem: 7976 1234 4567 123 3210 6789 - Disk: The output from
dfmight look like:Filesystem Size Used Avail Use% Mounted on /dev/sda1 100G 30G 65G 32% /
These commands provide a quick overview of system resource usage. Let me know if you need further details or examples!
