DEV Community

Cover image for 3 Bash One-Liners to Analyze Production Issues 10X Faster
Command Line Pirate 🏴‍☠️
Command Line Pirate 🏴‍☠️

Posted on

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 (1)

Collapse
 
josephj11 profile image
Joe

In #1, what does the while loop do? On my notebook, I get one line of output even though I have 2 cores and four threads.

#3 has me stumped.

bigbird@sananda:~/pq
$ free -m | awk '/Mem:/ {print $3}' | while read mem; do echo "$mem"; sleep 1; done | awk '{print "Memory Usage: " $1 "MB"}'
Memory Usage: 9622MB
bigbird@sananda:~/pq
$ free -m
              total        used        free      shared  buff/cache   available
Mem:          11928        9598         277         464        2052        1548
Swap:         12287        1602       10685
bigbird@sananda:~/pq
$ free -m | awk '/Mem:/ {print $3}'
9644
bigbird@sananda:~/pq
Enter fullscreen mode Exit fullscreen mode

I see two different memory values in the intermediary steps and the answer isn't either of them. It might just be a "race" condition between when I run the whole thing and the partial results. I don't see what the loop does in this one either.