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.

 
  • 77 Users Found This Useful
Was this answer helpful?

Related Articles

How To Setup Private Name Servers For VPS And Dedicated Servers?

For a VPS or Dedicated server, you may register your own nameservers (ns1.yourdomain.com and...

How can FFMPEG be installed on a Dedicated Server?

FFMPEG installation on a Dedicated Server-FFMPEG is an open source collection of libraries and...