<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Command Line Pirate 🏴‍☠️</title>
    <description>The latest articles on Forem by Command Line Pirate 🏴‍☠️ (@cli-pirate).</description>
    <link>https://forem.com/cli-pirate</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1001117%2Ffea9b81f-e0e4-4c83-bee3-e34050ee8b7a.jpg</url>
      <title>Forem: Command Line Pirate 🏴‍☠️</title>
      <link>https://forem.com/cli-pirate</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/cli-pirate"/>
    <language>en</language>
    <item>
      <title>3 Bash One-Liners to Analyze Production Issues 10X Faster</title>
      <dc:creator>Command Line Pirate 🏴‍☠️</dc:creator>
      <pubDate>Thu, 12 Dec 2024 00:48:33 +0000</pubDate>
      <link>https://forem.com/cli-pirate/3-bash-one-liners-to-analyze-production-issues-10x-faster-1ceg</link>
      <guid>https://forem.com/cli-pirate/3-bash-one-liners-to-analyze-production-issues-10x-faster-1ceg</guid>
      <description>&lt;p&gt;Life would be so much simpler if apps stopped failing in production, right? &lt;/p&gt;

&lt;p&gt;Since that's probably not happening in &lt;em&gt;this life&lt;/em&gt;, you need to get better at analyzing issues and make best use of tooling.&lt;/p&gt;

&lt;p&gt;Today, I’m sharing 3 bash scripts that will help you debug production issues: &lt;/p&gt;




&lt;h2&gt;
  
  
  #1: Analyze CPU Usage
&lt;/h2&gt;

&lt;p&gt;During development, have you ever run into following issues?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High CPU usage&lt;/li&gt;
&lt;li&gt;Slow response times&lt;/li&gt;
&lt;li&gt;Application Crashes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’ve experience running a production application I guess you answered yes.&lt;/p&gt;

&lt;p&gt;And the first place to look in such scenarios is your &lt;em&gt;server CPU usage&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You can use following bash script to monitor CPU usage over time and identify the processes eating resources and possibly causing the issue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;top &lt;span class="nt"&gt;-b&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 1 | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s1"&gt;'Cpu(s)'&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $2 + $4}'&lt;/span&gt; | &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nb"&gt;read &lt;/span&gt;cpu&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$cpu&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;sleep &lt;/span&gt;1&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;done&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print "CPU Usage: " $1 "%"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  #2: Analyze Network Usage
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Here’s quick script that provides a basic analysis of HTTP status codes, which can help identify common network errors and their frequency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-s&lt;/span&gt; https://example.com | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; &lt;span class="s1"&gt;'HTTP/[0-9.]* [0-9]*'&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $2}'&lt;/span&gt; | &lt;span class="nb"&gt;sort&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; | &lt;span class="nb"&gt;uniq&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; | &lt;span class="nb"&gt;sort&lt;/span&gt; &lt;span class="nt"&gt;-nr&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  #3: Analyze Memory Usage
&lt;/h2&gt;

&lt;p&gt;Given the log &amp;amp; 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. &lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;free &lt;span class="nt"&gt;-m&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'/Mem:/ {print $3}'&lt;/span&gt; | &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nb"&gt;read &lt;/span&gt;mem&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$mem&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;sleep &lt;/span&gt;1&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;done&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print "Memory Usage: " $1 "MB"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;And that’s it.&lt;/p&gt;

&lt;p&gt;Hope you’d find these commands useful while analyze production issues.&lt;/p&gt;

&lt;p&gt;Also, comment below which production issue you find hardest to debug?&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>bash</category>
      <category>cli</category>
    </item>
    <item>
      <title>3 Ready-to-Copy Bash Scripts to Visualize Python Code</title>
      <dc:creator>Command Line Pirate 🏴‍☠️</dc:creator>
      <pubDate>Wed, 11 Dec 2024 01:23:18 +0000</pubDate>
      <link>https://forem.com/cli-pirate/3-ready-to-copy-bash-scripts-to-visualize-python-code-49dm</link>
      <guid>https://forem.com/cli-pirate/3-ready-to-copy-bash-scripts-to-visualize-python-code-49dm</guid>
      <description>&lt;p&gt;Understanding code with a visual is 10x easier than from just reading it.&lt;/p&gt;

&lt;p&gt;Want to know how to create one quickly?&lt;/p&gt;

&lt;p&gt;Here are my 3 best bash scripts I use to visualize python code: &lt;/p&gt;

&lt;h2&gt;
  
  
  Visualize Code Structure
&lt;/h2&gt;

&lt;p&gt;You never know when you're going to run into your next overly complex code, and without a tool it's hard to spot code complexity. This can result in poor readability and risk of bugs as the project scales .&lt;/p&gt;

&lt;p&gt;But there's an easy way to do this with code structure graph. With &lt;code&gt;pyan3&lt;/code&gt;, you can quickly create a dependency of graph of your code and then use dot to render it as PNG image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pyan3 yourfile.py &lt;span class="nt"&gt;--dot&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; code-structure.dot &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; dot &lt;span class="nt"&gt;-Tpng&lt;/span&gt; code-structure.dot &lt;span class="nt"&gt;-o&lt;/span&gt; code-structure.png
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Visualize Control Flow
&lt;/h2&gt;

&lt;p&gt;Sometimes, in python codebase you'll find sections of code that are never executed due to conditional logic, loops or return statements. This redundant or forgotten code can lead to messy codebase, poor maintainability and potential bugs.&lt;/p&gt;

&lt;p&gt;With Control Flow Graphs you can easily identify such unreachable and dead code paths. Let &lt;code&gt;pycfg&lt;/code&gt; take care of the heavy lifting for you, by outputting control flow graph of your Python code as SVG file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pycfg yourfile.py &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; controlflow.svg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Visualize Module Dependencies
&lt;/h2&gt;

&lt;p&gt;While working with python modules, one issue that comes up is of circular dependency — where two or more modules reference each other in a loop. This generally leads to import errors and performance issues.&lt;/p&gt;

&lt;p&gt;Fortunately, with a module dependency graph, you can easily spot these problematic relationships. And with &lt;code&gt;snakefood&lt;/code&gt; tool, you can easily create one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sfood yourfile.py | dot &lt;span class="nt"&gt;-Tpng&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; module-dependencies.png
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that’s it.&lt;/p&gt;

&lt;p&gt;Hope you’d find these commands useful while creating visuals for Python code.&lt;/p&gt;

&lt;p&gt;Also, comment below which code visual you find the most helpful?&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>bash</category>
      <category>cli</category>
    </item>
    <item>
      <title>4 Commands to Debug “Silent Errors” in JavaScript</title>
      <dc:creator>Command Line Pirate 🏴‍☠️</dc:creator>
      <pubDate>Mon, 09 Dec 2024 17:09:16 +0000</pubDate>
      <link>https://forem.com/cli-pirate/4-commands-to-debug-silent-errors-in-javascript-2mlc</link>
      <guid>https://forem.com/cli-pirate/4-commands-to-debug-silent-errors-in-javascript-2mlc</guid>
      <description>&lt;p&gt;By silent errors here I mean an issue that doesn't produces any visible indication.&lt;/p&gt;

&lt;p&gt;Some common examples:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Missing Catch Blocks&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Long-Running Promises&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Shared State Race Conditions&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Erroneous Event Listeners&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s understand each error in detail and command to debug them:&lt;/p&gt;




&lt;h2&gt;
  
  
  #1: Debugging Missing Catch Blocks
&lt;/h2&gt;

&lt;p&gt;These errors occur when you miss attaching &lt;code&gt;.catch()&lt;/code&gt; handler to your promise. As a result when the promise is rejected, error isn’t surfaced. &lt;/p&gt;

&lt;p&gt;You can debug this error by running your code with &lt;code&gt;unhandled-rejections&lt;/code&gt; argument. It forces node to terminate the process on unhandled promise rejections, making the error explicit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;--unhandled-rejections&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;strict script.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  #2: Debugging &lt;strong&gt;Long-Running Promises&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Have you come across a Node.js code that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Never completes&lt;/li&gt;
&lt;li&gt;Consumes excessive memory over time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If yes, then it’s most likely caused by unresolved promise or infinite loop somewhere.&lt;/p&gt;

&lt;p&gt;You can validate the issue by limiting the execution time of the script like done below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;timeout &lt;/span&gt;10s node script.js &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Warning: Unresolved promise or infinite loop detected"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  #3: Debugging &lt;strong&gt;Shared State Race Conditions&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Shared State Race Conditions occur when multiple callbacks access shared state simultaneously.&lt;/p&gt;

&lt;p&gt;Due to the race condition, the program results in unpredictable outcome causing data inconsistencies without visible symptoms during testing.&lt;/p&gt;

&lt;p&gt;But fortunately node actually provides a &lt;code&gt;trace-async-hooks&lt;/code&gt; option to identify such execution patterns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;--trace-async-hooks&lt;/span&gt; script.js 2&amp;gt;&amp;amp;1 | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"asyncId"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  #4: Debugging &lt;strong&gt;Erroneous Event Listeners&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Finally let’s talk about errors within event listeners.&lt;/p&gt;

&lt;p&gt;These are caused by unhandled promise rejections within event listener callback. This leads to error never getting propagated to the main execution context.&lt;/p&gt;

&lt;p&gt;You can identify these errors by redirecting all node event logs to a grep filter to capture errors emitted during event handling&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;-r&lt;/span&gt; events script.js 2&amp;gt;&amp;amp;1 | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"Error"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;And That’s it.&lt;/p&gt;

&lt;p&gt;Hope you’d find these commands useful while debugging silent errors in JavaScript code.&lt;/p&gt;

&lt;p&gt;Also, comment below which silent error annoys you the most?&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>bash</category>
      <category>cli</category>
    </item>
    <item>
      <title>3 Bash Commands to Effectively Work With React Components</title>
      <dc:creator>Command Line Pirate 🏴‍☠️</dc:creator>
      <pubDate>Sun, 08 Dec 2024 18:27:12 +0000</pubDate>
      <link>https://forem.com/cli-pirate/3-bash-commands-to-effectively-work-with-react-components-mn3</link>
      <guid>https://forem.com/cli-pirate/3-bash-commands-to-effectively-work-with-react-components-mn3</guid>
      <description>&lt;p&gt;Working with React Components can get daunting at times especially with large codebases. &lt;/p&gt;

&lt;p&gt;In this post, I’m sharing 3 bash commands that I use to make some of that work easier.&lt;/p&gt;

&lt;p&gt;Let’s dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  #1: Find Components With Hardcoded Text
&lt;/h2&gt;

&lt;p&gt;For easier debugging you might have harcoded some values in code.&lt;/p&gt;

&lt;p&gt;But it’s always a good idea to get rid of them before productionization. As hard coding text makes localization difficult which becomes a barrier in globalizing the app.  &lt;/p&gt;

&lt;p&gt;You can use the following command to find hardcoded text, so that your app can support multiple languages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-Er&lt;/span&gt; &lt;span class="s2"&gt;"['&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;].*['&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;]"&lt;/span&gt; src/&lt;span class="k"&gt;**&lt;/span&gt;/&lt;span class="k"&gt;*&lt;/span&gt;.jsx | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s1"&gt;'i18n'&lt;/span&gt; | &lt;span class="nb"&gt;tee &lt;/span&gt;hardcoded_text.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  #2: identify Components Missing a Test File
&lt;/h2&gt;

&lt;p&gt;Another command which i regularly use to debug low test coverage.&lt;/p&gt;

&lt;p&gt;It’s to find what all components miss a test.&lt;/p&gt;

&lt;p&gt;Use this command to list all react components that are missing a test file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;find src &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s1"&gt;'*.jsx'&lt;/span&gt; | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s/.jsx$/.test.js/'&lt;/span&gt; | &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nb"&gt;read &lt;/span&gt;file&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$file&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Missing test: &lt;/span&gt;&lt;span class="nv"&gt;$file&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  #3: Check Deprecated Lifecycle Methods
&lt;/h2&gt;

&lt;p&gt;If you’re upgrading your React codebase to a new version, first problem you’ll face is of deprecated lifecycle method. &lt;/p&gt;

&lt;p&gt;Run the following bash command to proactively identify outdated code and smoother upgrades.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-Er&lt;/span&gt; &lt;span class="s1"&gt;'(componentWillMount|componentWillReceiveProps|componentWillUpdate)'&lt;/span&gt; src/&lt;span class="k"&gt;**&lt;/span&gt;/&lt;span class="k"&gt;*&lt;/span&gt;.jsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that’s it.&lt;/p&gt;

&lt;p&gt;Hope you’d find these commands useful while working with React components.&lt;/p&gt;

&lt;p&gt;Also, comment below which boring coding task are you procrastinating to automate?&lt;/p&gt;

</description>
      <category>react</category>
      <category>bash</category>
      <category>javascript</category>
      <category>cli</category>
    </item>
  </channel>
</rss>
