DEV Community

Cover image for 3 Bash One-Liners to Analyze Production Issues 10X Faster

3 Bash One-Liners to Analyze Production Issues 10X Faster

Life would be so much simpler if apps stopped failing in production, right?

Since that's probably not happening in this life, you need to get better at analyzing issues and make best use of tooling.

Today, I’m sharing 3 bash scripts that will help you debug production issues:


#1: Analyze CPU Usage

During development, have you ever run into following issues?

  • High CPU usage
  • Slow response times
  • Application Crashes

If you’ve experience running a production application I guess you answered yes.

And the first place to look in such scenarios is your server CPU usage.

You can use following bash script to monitor CPU usage over time and identify the processes eating resources and possibly causing the issue:

top -b -n 1 | grep 'Cpu(s)' | awk '{print $2 + $4}' | while read cpu; do echo "$cpu"; sleep 1; done | awk '{print "CPU Usage: " $1 "%"}'
Enter fullscreen mode Exit fullscreen mode

#2: Analyze Network Usage

To identify and fix network related performance bottlenecks—like slow load times, high latency, etc —analyze network traffic for requests, response times and errors. Fixing these will improve your website speed and user experience.

Here’s quick script that provides a basic analysis of HTTP status codes, which can help identify common network errors and their frequency.

curl -s https://example.com | grep -o 'HTTP/[0-9.]* [0-9]*' | awk '{print $2}' | sort -n | uniq -c | sort -nr
Enter fullscreen mode Exit fullscreen mode

#3: Analyze Memory Usage

Given the log & data tsunami that developers are experiencing today, it's critical to separate the signal from the noise. Without the ability to analyze server memory, it's impossible to debug memory leaks, high memory consumption and application crashes due to them.

Use following command to monitor memory usage over time and identify the processes consuming significant memory. You can also use it to optimize memory usage and improve application stability and performance.

free -m | awk '/Mem:/ {print $3}' | while read mem; do echo "$mem"; sleep 1; done | awk '{print "Memory Usage: " $1 "MB"}'
Enter fullscreen mode Exit fullscreen mode

And that’s it.

Hope you’d find these commands useful while analyze production issues.

Also, comment below which production issue you find hardest to debug?

Top comments (0)