Write me a bash script that gets the current cpu usage, memory usage, and uptime of the…
- User
Write me a bash script that gets the current cpu usage, memory usage, and uptime of the system
Assistant
Here is a bash script that will get the CPU usage, memory usage, and uptime of the system:
#!/bin/bash # Get CPU usage cpu_usage=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}') echo "CPU usage: $cpu_usage%" # Get memory usage memory_usage=$(free -m | awk 'NR==2{printf "%s/%sMB (%.2f%%)", $3,$2,$3*100/$2 }') echo "Memory usage: $memory_usage" # Get uptime uptime=$(uptime | awk '{print $3,$4}' | cut -f1-2 -d,) echo "Uptime: $uptime" # Print outputs echo echo "System status:" echo "-------------" echo "CPU usage: $cpu_usage%" echo "Memory usage: $memory_usage" echo "Uptime: $uptime"This script does the following:
- Uses
topto get the CPU usage and extracts the percentage usingawk - Uses
free -mto get the memory usage in MB and calculates the percentage - Uses to get the system uptime
- Uses
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.