Here is a nice little tool that I found in my travels, but has since become obscure due to the creator moving on to other endeavours. So what is a snapshot script? It is a bash script that runs every 10 minutes or so and records which processes are running. In the event of a system crash, you can look back at these logs and see what was running just prior to the crash and hopefully gain some insight as to what is happening with your system.

Since the original location of the script was here:
http://partytime.wackyfunster.com/leet/snapshot.sh.

I have made a copy of it here:
http://www.8bitpipe.com/tools/snapshot.sh

Here is an example of what real-world use might look like:


[root@8bitpipe ~]# wget www.8bitpipe.com/tools/snapshot.sh
–19:52:15– http://www.8bitpipe.com/tools/snapshot.sh
Resolving www.8bitpipe.com… 98.129.220.88
Connecting to www.8bitpipe.com|98.129.220.88|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 3160 (3.1K) [application/x-sh]
Saving to: snapshot.sh


100%[=======================================>] 3,160 –.-K/s in 0s


19:52:15 (1.51 MB/s) – snapshot.sh saved [3160/3160]


[root@8bitpipe ~]# chmod +x snapshot.sh
[root@8bitpipe ~]# mv snapshot.sh /usr/local/bin
[root@8bitpipe ~]# crontab -e


*/10 * * * * /usr/local/bin/snapshot.sh
crontab: installing new crontab

This will set the script to run once per 10 minutes, and archive the last hours worth of snapshots. The current snapshots will be stored in ~/.snapshot.cur

After a crash, this directory will be moved to ~/.snapshot.date (the date will be the first execution of the script after the server comes back up), and a symlink will be created from ~/.snapshot.last to the most recent set of snapshots.

As a note, if you need to change the commands executed by the script, just edit the section at the bottom (it’s marked within the script), and make sure any output is redirected to ~/.snapshot.cur/snapshot.1

This script is also useful if you need to back track and see what is utilizing resources even if the server isn’t crashing.

Happy hunting.

One Response to “Snapshot Script”
  1. mike says:

    Have you heard of Collectl? it’s very similar but has a lot information and lets you specify the snapshot granularity to as much as seconds!

    –mike

  2.  
Leave a Reply