title: Shell script for logging cpu and memory usage of a linux process
body: <p>I am looking for a way to log and graphically display cpu and RAM usage of linux processes over time. Since I couldn't find a simple tool to so (I tried zabbix and munin but installation failed) I started writing a shell script to do so</p>
  
  <p>The script file parses the output of top command through awk and logs into a csv file.  It</p>
  
  <ol>
  <li>Figures out the pid of the processes through ps command</li>
  <li>Uses top and awk to log cpu and memory usage.</li>
  </ol>
  
  <p>Here is how the script looks like</p>
  
  <pre><code>#!/bin/sh
  #A script to log the cpu and memory usage of linux processes namely - redis, logstash, elasticsearch and kibana
  
  REDIS_PID=$(ps -ef | grep redis | grep -v grep | awk '{print $2}')
  
  LOGSTASH_PID=$(ps -ef | grep logstash | grep -v grep | awk '{print $2}')
  
  ELASTICSEARCH_PID=$(ps -ef | grep elasticsearch | grep -v grep | awk '{print $2}')
  
  KIBANA_PID=$(ps -ef | grep kibana | grep -v grep | awk '{print $2}')
  
  LOG_FILE=/var/log/user/usage.log
  echo $LOG_FILE
  top -b | awk -v redis="$REDIS_PID" -v logstash="$LOGSTASH_PID" '/redis|logstash/ {print $1","$9","$10","$12}'
  </code></pre>
  
  <p>How do I </p>
  
  <ol>
  <li>Print the resource usage for multiple processes. Specifying multiple
  variables in the awk pattern is not working. It prints the usage for
  the first pid (redis in the above script) </li>
  <li>Print current timestamp when printing the resource details (through date +"%T")</li>
  <li>Print the process name along with the resource usage. Redis, Logstash, ElasticSearch or Kibana in the above case</li>
  <li>Redirect the above commands output to a log file. I tried > $LOG_FILE but it didn't work.</li>
  </ol>
  
  <p>Thoughts/Inputs?</p>
  
  <p>Thanks in advance.</p>