Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0125-preview· OpenAIHugging Face Arena Human Preference 55K

Write me a bash script that gets the current cpu usage, memory usage, and uptime of the…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-1’s answer.
  1. User

    Write me a bash script that gets the current cpu usage, memory usage, and uptime of the system

  2. Assistant

    Below is a simple Bash script that retrieves the current CPU usage, memory usage, and system uptime. This script uses common Linux utilities such as top, awk, free, and uptime to gather the necessary information.

    #!/bin/bash
    
    # Get CPU usage
    # Using top command with batch mode to get the CPU usage. The awk command extracts the usage percentage.
    CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
    
    # Get Memory Usage
    # Using free command to get memory usage statistics, then awk to parse the desired values and calculate the percentage.
    MEM_USAGE=$(free | grep Mem | awk '{print $3/$2 * 100.0"%"}')
    
    # Get System Uptime
    # Uptime command gives the system uptime directly.
    UPTIME=$(uptime -p)
    
    # Output the information
    echo "CPU Usage: $CPU_USAGE"
    echo "Memory Usage: $MEM_USAGE"
    echo "System Uptime: $UPTIME"
    

    Here's a breakdown of what each part does:

    1. CPU Usage: The script uses top with -bn1 options to run in batch mode and only one iteration to get the system's current CPU usage. It then parses the output using grep to find the line containing CPU usage information, which is then processed with awk and sed to extract and calculate the percentage of CPU usage.

    2. Memory Usage: It utilizes free to get the memory statistics, then grep to find the line starting with "Mem". With awk, it calculates the percentage of used memory by dividing the used memory ($3) by the total memory ($2) and multiplying by 100.

    3. System Uptime: The uptime -p command gives a human-readable output of how long the system has been running.

    How to Use the Script

    1. Copy the script into a file, for example, system_stats.sh.
    2. Give it execution permission with the command: chmod +x system_stats.sh.
    3. Run the script: ./system_stats.sh.

    Please note, this script is designed for Linux systems and might require adjustments or different commands for other Unix-like systems such as FreeBSD or macOS.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.

Identical first prompt