How to set up a logging script?

Setting up an automatic logging script and reviewing its results can save your Dedicated server from constant memory problems and unreasoned CPU usage spikes.

The given below sh script can be run every minute through cron to let you track considerable data within minutes that might be causing server crash/freeze.

Data gets saved in logging directory using the number for the minute as file name. In the script setup below, 60 files are created which get refreshed with new data every hour:

  • #!/bin/sh
  • TERM=vt100
  • export TERM
  • time=$(date)
  • min=${time:14:2}
  • top -b -n 1 > /home/my-home/log/$min
  • ps aux >> /home/my-home/log/$min
  • exit 0

It must be noted that running above script without any knowledge of logrotate can load up your server’s disk space in less than a second. The best solution is to set the script to save data to /tmp so that if this partition gets overfilled, it get cleaned up easily.


Radhe Dhakad is Support Executive and Network Admin at RackBank Datacenters Pvt. Ltd.. He is our curious techie who loves experimenting on servers and likes to pen down whatever he explores new on server technology!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© Copyright 2013-2021 RackBank© Datacenters Pvt. Ltd.

Data Center in India

Rated 4.7/5 based on 31 reviews at Google+