O'Reilly logo

Shell Scripting: Expert Recipes for Linux, Bash, and More by Steve Parker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Recipe 18-2: CSV Formatting

Systems administration is often very reactive, and there often isn’t time to design, write, and test a clean and tidy shell script. This recipe deals with a memory leakage problem; sometimes the best solution in this situation is to create a very quick and simple script that grabs the relevant data to be analyzed later. The important thing is to get timely data, not how tidy or nicely formatted the data (or even the script) is. grab-meminfo.sh is an example of such a script; it simply grabs the timestamp and a copy of /proc/meminfo three times a minute. It is better to save more data than you need than to discard details that will later be useful.

Technologies Used

  • /proc/meminfo
  • ((suffix++))
  • CSV
  • bc

Concepts

/proc/meminfo is one of many files under /proc that appear to be text files but are actually a direct interface to the current running Linux kernel. These are an excellent resource for shell scripts to acquire raw data from the kernel in an easy-to-use format, without relying on additional utilities (such as free), which take the same data and reinterpret it.

The plot-graph.sh script takes the raw data from /proc/meminfo and formats it into a CSV file, which can be read by spreadsheet software such as Microsoft Excel, OpenOffice.org, or LibreOffice. This desktop software can be used to format the data into a graph, which can help to visualize the memory usage over time. It also saves the data out in a more concise format. Finally, stats.sh uses ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required